M
Morty
Hello,
I am trying to implement multithreading in java for the first time, and I
find that my threads do not want to share system resources, I cannot get
them to run concurrently. Each thread insists on finishing all the way
before another wants to execute, despite my attempts at wait(), sleep() or
yield().
I have extracted a simple demonstration of my problem below:
package trypo.apps.testarea;
public class Test2 {
public static void main(String[] args) throws InterruptedException {
TestThread a = new TestThread("Test1", 10, 13);
TestThread b = new TestThread("Test2", 20, 23);
a.join();
b.join();
}
}
Class Test2 starts 2 separate threads that should count up towards a
maxvalue.
package trypo.apps.testarea;
public class TestThread extends Thread {
private int countValue;
private int maxValue;
public TestThread(String newName, int InitValue, int maxValue) {
super(newName);
this.countValue = InitValue;
this.maxValue = maxValue;
this.run();
}
public void run() {
while (this.countValue < this.maxValue) {
System.out.println(this.getName()+" value "+this.countValue);
this.countValue++;
yield();
}
}
}
Class TestThread is an extension of Thread which performs an "endless" loop
in run(), however for each loop it calls yield() to give the other thread a
chance to run.
I get the following output:
Test1 value 10
Test1 value 11
Test1 value 12
Test2 value 20
Test2 value 21
Test2 value 22
....which tells me that thread Test1 completes its run() method completely
before Test2 even got a single loop done. This holds true also if I add more
threads, and/or a wider count interval - the thread that starts first
finishes before anything else can start followed by the next and so on. I
specifically need my threads to execute "simulatenously" so they can
exchange information and go about their separate business most of the time.
I am initially trying to execute this in Eclipse 3.2.2, but I have attempted
to execute the code from the prompt (I am running WinXP), with the same
result.
This is my version info:
C:\>java -version
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)
Can you help me spot what i'm missing here ?
Thanks in advance !!
Morten
I am trying to implement multithreading in java for the first time, and I
find that my threads do not want to share system resources, I cannot get
them to run concurrently. Each thread insists on finishing all the way
before another wants to execute, despite my attempts at wait(), sleep() or
yield().
I have extracted a simple demonstration of my problem below:
package trypo.apps.testarea;
public class Test2 {
public static void main(String[] args) throws InterruptedException {
TestThread a = new TestThread("Test1", 10, 13);
TestThread b = new TestThread("Test2", 20, 23);
a.join();
b.join();
}
}
Class Test2 starts 2 separate threads that should count up towards a
maxvalue.
package trypo.apps.testarea;
public class TestThread extends Thread {
private int countValue;
private int maxValue;
public TestThread(String newName, int InitValue, int maxValue) {
super(newName);
this.countValue = InitValue;
this.maxValue = maxValue;
this.run();
}
public void run() {
while (this.countValue < this.maxValue) {
System.out.println(this.getName()+" value "+this.countValue);
this.countValue++;
yield();
}
}
}
Class TestThread is an extension of Thread which performs an "endless" loop
in run(), however for each loop it calls yield() to give the other thread a
chance to run.
I get the following output:
Test1 value 10
Test1 value 11
Test1 value 12
Test2 value 20
Test2 value 21
Test2 value 22
....which tells me that thread Test1 completes its run() method completely
before Test2 even got a single loop done. This holds true also if I add more
threads, and/or a wider count interval - the thread that starts first
finishes before anything else can start followed by the next and so on. I
specifically need my threads to execute "simulatenously" so they can
exchange information and go about their separate business most of the time.
I am initially trying to execute this in Eclipse 3.2.2, but I have attempted
to execute the code from the prompt (I am running WinXP), with the same
result.
This is my version info:
C:\>java -version
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)
Can you help me spot what i'm missing here ?
Thanks in advance !!
Morten