[136] in java-interest
Thread Implementation in Java
daemon@ATHENA.MIT.EDU (Conor Cunningham)
Sun May 28 20:31:27 1995
Date: Sun, 28 May 1995 09:29:39 -0500 (CDT)
From: Conor Cunningham <conroy@cs.utexas.edu>
To: java-interest@java.Eng.Sun.COM
-----BEGIN PGP SIGNED MESSAGE-----
I'm an undergraduate CS student, and I am going to be doing some work
with java in the Fall - I'm familiarizing myself with the language, and I
have a question about how the threads work.
Does the interpreter preemptively multitask the threads (on Solar. 2.4)?
I wrote a couple of test programs to see, and it appears that the
interpreter will switch when I call yield() explicitly.
Here is version 1 of the code:
class T1 extends Thread {
private int num_of_thread; //Number of the Thread
//Constructor
T1(int a) {
num_of_thread = a;
}
public void run() {
int count = 0;
while (count++ < 1000)
{
System.out.println(num_of_thread);
}
}
}
class TestThread {
public static void main(String argv[]) {
T1 obj1 = new T1(1);
T1 obj2 = new T1(2);
obj1.start();
obj2.start();
}
}
// End version 1
This code just spawns 2 threads and each prints their id number for me to
the screen. The output from this program was 1000 "1"s followed by 1000
"2"s. I expected the thread output to be a bit more intermingled.
I made a version 2 that used a much larger loop constant in the case that
the interpreter switched threads infrequently - that did not seem to do
the trick, however.
My searches through the documentation of the API and the programmers
guide have not given me insight as to where I have gone wrong. If this
is located in some of the documentation, could someone point me in the
right direction? If it isn't in the docs, could someone explain a little
more about how the threading model works?
I'm interested mostly in why the threads appear to be acting in this
manner. Is it an I/O behavior or perhaps dependent upon the machine
which I am using?
Any help appreciated. I have been very impressed with the language, and
I hope that devlopment continues.
Conor Cunningham
[Moderator: Java threads are defined to be priority preemptive, but are
not defined to be time-sliced within a priority -- or rather, within a
priority it is undefined in what order runnable threads run.
Your particular example behaves the way it does on the Solaris system
because of details of the threads package there: on that system, there
is no time slicing within a priority, so the first thread to run will
keep running unless it has to wait for something (which it doesn't).
The important point to understand is that to depend on that behavior
would be wrong. On Win32, where we use native, time-sliced threads,
you would get something like alternating series of 1's and 2's.
It's up to the applet writer to understand what is and isn't guaranteed
by the threads package in order to write robust, portable applets. It
is up to us to make sure that we clearly document what is and isn't
guaranteed!]
-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
Comment: Fight the MAN, use PGP.
iQCVAwUBL8iIyaxpupv2dEElAQH2FgQAxp0hIvmCO6ScgnGitJzhu8jyyqt3UK75
2e+lU+VoOCEQILs4gEemBg/SVMHjUcUN+cp4R+o9xogm6FSNfJpdMXtA3m0eNR7k
+2mYVoARqyuUueJA6JfbRb8ZPFP/3oRAwh3vqQdkUtOCopx0QeiiRPaUhqLGHDhR
U0XbS8iqxoc=
=CwBK
-----END PGP SIGNATURE-----
Conor Cunningham | CS Senior | UT ACM President, Spring 1995
<conroy@cs.utexas.edu> |FIGHT THE MAN| University of Texas at Austin
---< Hey Bill: >---------| USE PGP |--------------------------------
"You can have my private key when you pry it from my cold, dead hands."
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com