Note that there are some explanatory texts on larger screens.

plurals
  1. POThread wait sleep and notify
    text
    copied!<p>i'm creating a simple timer,i have some basic knowledge about thread. i can stop the currently sunning thread by wait but can't able to notify it shows"illegalmonitorstateexception".</p> <pre><code>'import java.awt.*; import java.awt.event.*; import java.applet.Applet; /* &lt;applet code = "TimerClass" width = 800 height = 600&gt;&lt;/applet&gt;*/ public class TimerClass extends Applet implements Runnable, ActionListener { Thread t1; int i; TextField sec; Button start; Button stop; boolean isStop; boolean isAlive; public void init() { sec = new TextField(10); start = new Button("Start"); stop = new Button("Stop"); add(sec); add(start); add(stop); start.addActionListener(this); stop.addActionListener(this); isStop = true; t1 = new Thread(this); } public void startThread() { t1.start(); isAlive = true; } public void awake() { try { System.out.println(Thread.currentThread().isAlive()); synchronized(t1) { Thread.currentThread().notify(); } } catch(Exception e) { System.out.println(e.toString()); } //Thread.interrupted(); } synchronized public void run() { do { try { Thread.sleep(1000); i += 1; sec.setText(Integer.toString(i)); System.out.println(i); if(isStop) { break; } } catch(InterruptedException ie) { continue; } }while(isStop != true); } public void stopThread() { try { synchronized(t1) { Thread.currentThread().wait(); } } catch(Exception ie) { } } public void paint(Graphics g) { } public void actionPerformed(ActionEvent ae) { if(ae.getActionCommand() == "Start") { if(!isAlive) { isStop = false; System.out.println("Thread start"); startThread(); } else { isStop = false; System.out.println("Thread start again" + isStop); awake(); } } if(ae.getActionCommand() == "Stop") { isStop = true; stopThread(); } } } </code></pre> <p>need help with this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload