🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1941

Re: Programming Threads

#1941
From: Yonatan Taub
Date: Mon, 16 Jul 2001 07:10
114 lines
3660 bytes
The reason suspend() and resume() are deprecated is because when you invoke
them, the executing thread does not release the lock.
Therefore it is dead-lock prone and risky.

I don't think that otherwise you can "suspend" and "resume" a thread exactly
from where you "stopped" it.
The following example illustrates a thread doing its work in a while loop.
Inside the loop, the executing thread has a boolean flag indicating whether
to snooze.
I hope this example will help.




import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class ThreadTest {

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = frame.getContentPane();

    JButton stop  = new JButton("stop");
    JButton start = new JButton("Start");
    JButton exit  = new JButton("Exit");

    final DoSomething t = new DoSomething();
    t.start();

    // anonymous inner class
    stop.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        t.snooze();
      }
    });

    // anonymous inner class
    start.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        t.wakeUp();
      }
    });

    // anonymous inner class
    exit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        t.stopRequest();
      }
    });



    cp.setLayout(new FlowLayout());
    cp.add(stop);
    cp.add(start);
    cp.add(exit);

    frame.setSize(300, 300);
    frame.setVisible(true);
  }

}


class DoSomething extends Thread {

  private volatile boolean noStopRequested = true;
  private volatile boolean isSnoozeTime    = false;
  private static int counter;

  public void run() {
    while(noStopRequested)
    {
      System.out.println("counter = " + counter);
      counter++;

      if(isSnoozeTime)
      {
        synchronized(this)
        {
          System.out.println("About to snooze");
          try { wait(); }
          catch(InterruptedException x) { x.printStackTrace(); }
          System.out.println("was notified - returned from wait()");
        }
      }
    }
  }


  public void stopRequest() {
    isSnoozeTime    = false;
    noStopRequested = false;

    // give a chance to the thread to die peacefuly.
    try { Thread.currentThread().sleep(200); }
    catch(InterruptedException x) { x.printStackTrace(); }
    this.interrupt();
  }


  public synchronized void wakeUp() {
    isSnoozeTime = false;
    this.notify();
  }


  public synchronized void snooze() { isSnoozeTime = true; }

}
Technical Details
Message-ID: <3b5284b6_1@dnews>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news-peer.gip.net!news.gsl.net!gip.net!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3b4d9294@news.gwi-ag.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,df9256bde7b56add
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-07-15 23:09:17 PST
From: "Yonatan Taub" <yonatanta@bashan.co.il>
Newsgroups: borland.public.jbuilder.java.api
References: <3b4d9294@news.gwi-ag.com>
Subject: Re: Programming Threads
Date: Mon, 16 Jul 2001 09:10:54 +0200
Lines: 116
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 212.179.22.2
Message-ID: <3b5284b6_1@dnews>
X-Trace: dnews 995263670 212.179.22.2 (15 Jul 2001 23:07:50 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!news-peer.gip.net!news.gsl.net!gip.net!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews