Article View: borland.public.jbuilder.java.language
Article #7411Re: Fun with threads
From: Sean Capstick
Date: Thu, 21 Jun 2001 20:43
Date: Thu, 21 Jun 2001 20:43
54 lines
2538 bytes
2538 bytes
Thanks but that won't work because now the main code is sitting there
waiting for the thread to finish rather than carry on with it's own work.
Under Windows, I could have the thread throw a message out which would get
caught by the main program's message handler. I don't see how to emulate
this.
Sean
----- Original Message -----
From: Yonatan Taub <yonatanta@bashan.co.il>
Newsgroups: borland.public.jbuilder.java.language
Sent: Thursday, June 21, 2001 2:22 PM
Subject: Re: Fun with threads
> class DoSomeWorkThread extends Thread {
>
> Object lock;
>
> public DoSomeWorkThread(Object lock) { this.lock = lock; }
> public void run() {
> // simulate some lenghy calculations.
> System.out.println("in worker thread - about to do some lengthy
> operation.");
> try { sleep(4000); }
> catch(Exception x) {}
> System.out.println("Finished processing and about to notify main
> thread.");
> synchronized(lock) {
> lock.notify();
> }
> }
> } // end class DoSomeWork
>
> public class MainWait {
>
> public static Object lock = new Object();
>
> public static void main(String[] args) {
> Thread t = new DoSomeWorkThread(lock);
> t.start();
> synchronized(lock) {
> try {
> System.out.println("main thread is about to wait.");
> lock.wait();
> } catch(InterruptedException x) {}
> }
> System.out.println("Main thread has been notified and can do some
> finishing stuff.");
> }
> } // end class MainWait
>
>
>
Technical Details
Message-ID:
<3b325b71_1@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3b320ce8_1@dnews> <3b322cbc$1_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,2fd1c7b03719c733 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-06-21 13:42:15 PST From: "Sean Capstick" <nospam.scapstick@dimensions.ca> Newsgroups: borland.public.jbuilder.java.language References: <3b320ce8_1@dnews> <3b322cbc$1_1@dnews> Subject: Re: Fun with threads Date: Thu, 21 Jun 2001 16:43:19 -0400 Lines: 57 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2314.1300 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 NNTP-Posting-Host: 207.139.208.137 Message-ID: <3b325b71_1@dnews> X-Trace: dnews 993155953 207.139.208.137 (21 Jun 2001 13:39:13 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!ihug.co.nz!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!dnews