🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #15759

Re: Let sleeping threads lie?

#15759
From: Michael A. Long
Date: Tue, 27 Apr 1999 00:00
143 lines
4771 bytes
Just an addendum to what Chris said on this subject.  It is not really
advisable to us notify(), it can lead to deadlocks if no thread is ready to
activate.  notifyAll() notify's all threads that the action has completed.
For what it's worth.

Tom Heagy wrote in message <37262668.3C79A815@sciatl.com>...
>Thanks Chris.  That works like a charm.  Just out of curiosity, could you
>tell me what the intent of "notify" is?  After doing some more reading I'm
>still not clear, but it seems like it may have something to do with objects
>that are blocked due to waiting on a response from another thread???
>
>-Tom
>
>Chris Hill wrote:
>
>> 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: <7g65lk$ksf4@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <37261640.457FA257@sciatl.com> <37262022.3697507684@forums.borland.com> <37262668.3C79A815@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: "Michael A. Long" <mlong@ecst.csuchico.edu>
Subject: Re: Let sleeping threads lie?
Date: 1999/04/27
Message-ID: <7g65lk$ksf4@forums.borland.com>
X-Deja-AN: 471760550
References: <37261640.457FA257@sciatl.com> <37262022.3697507684@forums.borland.com> <37262668.3C79A815@sciatl.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Organization: California State University, Chico
Newsgroups: borland.public.jbuilder.java.language