Article View: borland.public.jbuilder.java.language
Article #15902Re: Let sleeping threads lie?
From: Chris Hill
Date: Tue, 27 Apr 1999 00:00
Date: Tue, 27 Apr 1999 00:00
127 lines
3640 bytes
3640 bytes
You should use the Thread.interrupt method instead of Object.notify.
Here is a sample:
// BEGIN wakethr.java
import java.awt.*;
import java.awt.event.*;
public class wakethr extends Frame
{
public static void main(String args[])
{
Frame MyFrame = new wakethr();
MyFrame.show();
}
Button wakeBut = new Button();
Button exitBut = new Button();
SleeperThread MySleeper = new SleeperThread();
wakethr()
{
exitaction quit = new exitaction();
setTitle("Interrupt Thread Demo");
setSize(400,100);
setLocation(100,100);
setLayout(new BorderLayout());
addWindowListener(quit);
wakeBut.setLabel("Wake Thread");
wakeBut.addActionListener(new wakeaction(MySleeper));
add(wakeBut,BorderLayout.CENTER);
exitBut.setLabel("Exit");
exitBut.addActionListener(quit);
add(exitBut,BorderLayout.EAST);
MySleeper.start();
}
}
class exitaction extends WindowAdapter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class wakeaction implements ActionListener
{
Thread wakeThread;
wakeaction(Thread t)
{
wakeThread = t;
}
public void actionPerformed(ActionEvent e)
{
wakeThread.interrupt();
}
}
class SleeperThread extends Thread
{
public void run()
{
while (true) {
System.out.println("I'm going to sleep for 10 seconds.");
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
System.out.println("My sleep was interrupted.");
}
System.out.println("I'm awake!!");
}
}
}
// END wakethr.java
On Tue, 27 Apr 1999 15:55:44 -0400, Tom Heagy <tom.heagy@sciatl.com>
wrote:
>I'm trying to find a way to wake up a sleeping thread and I'm missing
>something that is probably obvious to someone else.
>
>I have a little test project where I've created an object (Sleeper) that
>extends "Thread" and does nothing more than the following in it's run
>method:
>
> while (true) {
> System.out.println("I'm going to sleep for 10 seconds.");
> try {
> Thread.sleep(10000);
> }
> catch (InterruptedException e) {
> System.out.println("My sleep was interrupted.");
> }
> System.out.println("I'm awake!!");
> }
>
>Nothing special here and this obviously works as desired. It wakes up
>every 10 seconds and prints it's messages. Now, my problem comes into
>play when I want to wake up the thread BEFORE it does itself. I thought
>this was possible through the "notify" method. I have another class
>(with a Main method) that instantiates a Sleeper object and executes the
>"notify" method when a user clicks a button. When I do this however, I
>get the message that states "Current thread not owner".
>
>I guess my bottom line is- "How do I become the owner of this thread?"
>I own the original handle to the sleeper object. What else do I need to
>do?
>
>Thanks in advance for your input.
>
>-Tom
>
Chris Hill
Chris244@aol.com
Technical Details
Message-ID:
<37262022.3697507684@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37261640.457FA257@sciatl.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,efa12c4b85d88a4c X-Google-Attributes: gidf74b3,public From: Chris244@aol.com (Chris Hill) Subject: Re: Let sleeping threads lie? Date: 1999/04/27 Message-ID: <37262022.3697507684@forums.borland.com> X-Deja-AN: 471610541 References: <37261640.457FA257@sciatl.com> Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language