Threading... what am I doing wrong? =(
I want to have two threads execute concurrently, but it looks like the program is running sequentially... what did I do wrong? =/
import java.util.Date;
publicclass TestClassimplements Runnable{
protected Thread thread;
publicstaticvoid main(String[] arg){new TestClass();}
public TestClass(){ thread =new Thread(this); thread.start();}
// set up two threads, and have thread 1 modify thread 2
publicvoid run(){
ThreadClass t1 =new ThreadClass("thread 1");
ThreadClass t2 =new ThreadClass("thread 2");
t1.checkthreading(t2);
System.out.println("["+new Date().getTime()
+"] done in main class");}
privateclass ThreadClassimplements Runnable{
private Thread thread;
private String string;
public ThreadClass(String string){
this.string=string;
thread=new Thread(this);
thread.start();}
publicvoid run(){
while(true){
try{ Thread.sleep(200);}
catch (InterruptedException e){}}}
publicvoid setString(String string){ this.string=string;}
public String getString(){return string;}
publicvoid checkthreading(ThreadClass other){
try{ Thread.sleep(5000);}
catch (InterruptedException e){}
other.setString(string+string);
System.out.println("["+new Date().getTime()
+"] done in checkthreading");}}
}
What I expected was that the main class creates the two tread objects, and because t1 is a thread, there is no "waiting for" the call to t1.checkthreading(t2) to be done, instead immediately continuing executing to print the "done in main class" statement. Instead, the main class waits for the threads to completely finish up before continuous execution....
can anyone explain why? =(
- Mike

