Threads : wait() , notify() instead of suspend() and resume() ?

package threads.src02;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

publicclass Demo2extends JFrame{

privatestaticfinallong serialVersionUID = 1;

private JPanel jContentPane =null;

private JTextField jTextField =null;

private JPanel jPanel =null;

private JButton jBStart =null;

private JButton jBStop =null;

private MyThread thread =null;

public Demo2(){

super();

initialize();

}

publicstaticvoid main(String[] args){

Demo2 demo =new Demo2();

demo.setVisible(true);

}

privatevoid initialize(){

this.setSize(200, 100);

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

}

private JPanel getJContentPane(){

if (jContentPane ==null){

jContentPane =new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getJTextField(), BorderLayout.NORTH);

jContentPane.add(getJPanel(), BorderLayout.CENTER);

}

return jContentPane;

}

private JTextField getJTextField(){

if (jTextField ==null){

jTextField =new JTextField();

}

return jTextField;

}

private JPanel getJPanel(){

if (jPanel ==null){

jPanel =new JPanel();

jPanel.setLayout(new FlowLayout());

jPanel.add(getJBSart(),null);

jPanel.add(getJBStop(),null);

}

return jPanel;

}

private JButton getJBSart(){

if (jBStart ==null){

jBStart =new JButton("Start");

}

jBStart.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

if(thread ==null)

thread =new MyThread(Demo2.this);

else

thread.resume();

}

});

return jBStart;

}

private JButton getJBStop(){

if (jBStop ==null){

jBStop =new JButton("Stop");

}

jBStop.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

thread.suspend();

}

});

return jBStop;

}

publicvoid printMessage(int val){

jTextField.setText(Integer.toString(val));

}

}

class MyThreadextends Thread{

private Demo2 target;

public MyThread(Demo2 o){

this.target = o;

this.start();

}

publicvoid run(){

int i = 0;

while(true)

try{

target.printMessage(i++);

sleep(100);

}catch (InterruptedException e){

e.printStackTrace();

}

}

}

How to do the same using wait() , notify() instead of suspend(), resume() ?

[7313 byte] By [Del2a] at [2007-11-27 0:57:10]
# 1

> How to do the same using wait() , notify() instead of suspend(),

> resume() ?

You should find information in the What should I use instead of Thread.suspend and Thread.resume? section of [url=http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html]Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?[/url]

TimTheEnchantora at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 2
It's so interesting but i don't see examples like my (above). Please help me remake my example.Message was edited by: Del2
Del2a at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 3
What about implementing the threadSuspended flag mechanism in your MyThread class ?
TimTheEnchantora at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 4
I know that I shoud use boolean flag .. I' am trying but without success give me simple example .. like mine.
Del2a at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 5
> I know that I shoud use boolean flag .. I' am trying> but without success give me simple example .. like> mine.well, you haven't got a flag anywhere
georgemca at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 6

> well, you haven't got a flag anywhere

package threads.src02;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Demo2 extends JFrame {

private static final long serialVersionUID = 1;

private JPanel jContentPane = null;

private JTextField jTextField = null;

private JPanel jPanel = null;

private JButton jBStart = null;

private JButton jBStop = null;

private MyThread thread = null;

private boolean flag = false;

public Demo2() {

super();

initialize();

}

public static void main(String[] args) {

Demo2 demo = new Demo2();

demo.setVisible(true);

}

private void initialize() {

this.setSize(200, 100);

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

}

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getJTextField(), BorderLayout.NORTH);

jContentPane.add(getJPanel(), BorderLayout.CENTER);

}

return jContentPane;

}

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

}

return jTextField;

}

private JPanel getJPanel() {

if (jPanel == null) {

jPanel = new JPanel();

jPanel.setLayout(new FlowLayout());

jPanel.add(getJBSart(), null);

jPanel.add(getJBStop(), null);

}

return jPanel;

}

private JButton getJBSart() {

if (jBStart == null) {

jBStart = new JButton("Start");

}

jBStart.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(thread == null)

thread = new MyThread(Demo2.this);

}

});

return jBStart;

}

private JButton getJBStop() {

if (jBStop == null) {

jBStop = new JButton("Stop");

}

jBStop.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

stop();

}

});

return jBStop;

}

synchronized public void put(int val) {

if(flag)

try {

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

flag = false;

jTextField.setText(Integer.toString(val));

//notify();

}

synchronized public void stop(){

flag = true;

}

}

class MyThread extends Thread {

private Demo2 target;

public MyThread(Demo2 o) {

this.target = o;

this.start();

}

public void run() {

int i = 0;

while(true)

target.put(i);

}

}

Wat's wrong ?

Del2a at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 7
up!
Del2a at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 8
up!
Del2a at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 9

> Wat's wrong ?

Why didn't you implement the mechanism in your MyThread class ?

(it's the thread that is supposed to be suspended after all, not the frame, right ?)

a) Declare a boolean instance field threadSuspended

b) modify the run() method as described in the documentation from reply#1 (i.e. avoid doing anything while threadSuspended is true)

c) add a way to manage the suspended status of the thread (inspired by the doc also), something like: synchronized public void setSuspended(boolean suspended) {

threadSuspended = suspended;

if (!threadSuspended) {

notify();

}

}

TimTheEnchantora at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 10

Hai,

I moidfied your code and replaced suspend() and resume() with wait() and notify().

Please find the modified source below.

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Demo2 extends JFrame {

private static final long serialVersionUID = 1;

private JPanel jContentPane = null;

private JTextField jTextField = null;

private JPanel jPanel = null;

private JButton jBStart = null;

private JButton jBStop = null;

private MyThread thread = null;

private boolean isStopFlag = false;

public Demo2() {

super();

initialize();

}

public static void main(String[] args) {

Demo2 demo = new Demo2();

demo.setVisible(true);

}

private void initialize() {

this.setSize(200, 100);

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

}

private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getJTextField(), BorderLayout.NORTH);

jContentPane.add(getJPanel(), BorderLayout.CENTER);

}

return jContentPane;

}

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

}

return jTextField;

}

private JPanel getJPanel() {

if (jPanel == null) {

jPanel = new JPanel();

jPanel.setLayout(new FlowLayout());

jPanel.add(getJBSart(), null);

jPanel.add(getJBStop(), null);

}

return jPanel;

}

private JButton getJBSart() {

if (jBStart == null) {

jBStart = new JButton("Start");

}

jBStart.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

if(thread == null)

thread = new MyThread(Demo2.this);

else

thread.startCounter();

}

});

return jBStart;

}

private JButton getJBStop() {

if (jBStop == null) {

jBStop = new JButton("Stop");

}

jBStop.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

isStopFlag = true;

}

});

return jBStop;

}

public void printMessage(int val) {

jTextField.setText(Integer.toString(val));

}

class MyThread extends Thread {

private Demo2 target;

public MyThread(Demo2 o) {

this.target = o;

this.start();

}

public void run() {

int i = 0;

while(true)

try {

if (isStopFlag){

synchronized(this){

wait();

isStopFlag = false;

}

}

target.printMessage(i++);

sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public void startCounter()

{

synchronized(this){

notify();

}

}

}

}

hope this might be helpful to you.

feel free to ask me if you have any doubts regarding this

pavandv.forum@gmail.com

pavandva at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...
# 11

Well the thread is a month old but you may as well get it right:

> public void actionPerformed(ActionEvent e) {

>isStopFlag = true;

> }

public void actionPerformed(ActionEvent e) {

thread.stopCounter();

}

>private boolean isStopFlag = false;

private volatile boolean isStopFlag = false;

>public void run() {

>int i = 0;

>while(true)

> try {

> if (isStopFlag){

> synchronized(this){

> wait();

> isStopFlag = false;

> }

> }

> target.printMessage(i++);

> sleep(100);

> } catch (InterruptedException e) {

> e.printStackTrace();

>}

> }

public void run() {

int i = 0;

try {

synchronized(this){

while (isStopFlag)

wait();

}

target.printMessage(i++);

sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

> public void startCounter()

> {

> synchronized(this){

> notify();

> }

> }

public void stopCounter

{

isStopFlag = true;

}

public void startCounter()

{

synchronized(this){

isStopFlag = false;

notify()All;

}

}

ejpa at 2007-7-11 23:30:36 > top of Java-index,Java Essentials,Java Programming...