Article View: aus.computers.java
Article #395Re: THREADS AND SYNCHRONISATION
From: Paul Murray
Date: Sat, 02 Dec 2000 00:00
Date: Sat, 02 Dec 2000 00:00
66 lines
3176 bytes
3176 bytes
"Qteemoi" <qteemoi@alphalink.com.au> wrote in message
news:3A265709.C295849B@alphalink.com.au...
> haha..i would be, but u see thats the problem, i dun quite understand
> anything of it to be more specific.....i dun think i can quite grasp the
> concept...everytime i think of a thread i think of a process....
A thread is like a process that runs within an operating system "process".
When you run "java.exe", there is one windows process (a java virtual
machine), but this JVM may run several java "threads".
A thread, essentally, is a stack context - a current point of execution.
When any line of code is getting executed, some thread is executing.
So where do the threads come from?
As I understand it ... java.exe is a bit of "C" code. It starts up a Java
Virtual Machine (which lives in some DLSs or the equivalent), then it tells
the JVM to kick off a thread that starts at the "main" method of the class
you passed as a parameter.
Now you can make up threads of your own and kick them off. From your POV as
a java programmer, these all run simultaneously. To make a thread, do a
Thread t = new Thread();
To kick it off, you do a
t.start().
Once you do that, the thread will start running. 'Course, you can't simply
go new Thread(); You either subclasds the thread object, or more commonly
construct it with an instance of a Runnable
class Counter implements Runnable {
final int myNum;
Counter(int n) {myNum = n; }
public void run() {
for(int i = 0; i<100; i++) {
System.out.println("I am Counter " + myNum + ", and this is
iteration " + i);
}
}
}
public class foo {
public static void main(String[] av) {
for(int i = 0; i<10;i++) {
Counter c = new Counter(i);
Thread t = new Thread(c);
t.start();
}
}
}
Now if you run this, all 10 threads will run at once and make a jumble on
system.out. Try it.
Once you've done that, we can diuscuss synchonisation, or you might have
enough of a start now to work it out on your own. Note that the bahaviour of
wait() and notify() can be subtle, so yo may need to come back here to talk
about it.
--------------------------------------------------------------
Repent! Quit your job! Slack off!
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Technical Details
Message-ID:
<ai_V5.150449$e5.92887@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3A22F646.F7CD61C9@alphalink.com.au> <UXlV5.146676$e5.87865@newsfeeds.bigpond.com> <3A265709.C295849B@alphalink.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,606181f3a363975d X-Google-Attributes: gid10d6db,public From: "Paul Murray" <pmurray@nospam.bigpond.com> Subject: Re: THREADS AND SYNCHRONISATION Date: 2000/12/02 Message-ID: <ai_V5.150449$e5.92887@newsfeeds.bigpond.com> X-Deja-AN: 700177850 References: <3A22F646.F7CD61C9@alphalink.com.au> <UXlV5.146676$e5.87865@newsfeeds.bigpond.com> <3A265709.C295849B@alphalink.com.au> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.3018.1300 X-Trace: newsfeeds.bigpond.com 975728902 203.54.250.232 (Sat, 02 Dec 2000 14:48:22 EST) Organization: Telstra BigPond Internet Services (http://www.bigpond.com) X-MSMail-Priority: Normal NNTP-Posting-Date: Sat, 02 Dec 2000 14:48:22 EST Newsgroups: aus.computers.java