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

[4121 byte] By [MikeKamermansa] at [2007-11-27 2:28:39]
# 1

you're calling that checkThreading from the main thread, and hence, indirectly, calling Thread.sleep from the main thread (it doesn't matter that the method is contained in a Runnable, it's still being invoked from the main thread). so the main thread goes to sleep for 5 seconds, by which time everything else is finished

georgemca at 2007-7-12 2:41:04 > top of Java-index,Java Essentials,New To Java...
# 2
Is there a way to do outside-of-class calls for methods with this kind of implicit sleep? I'm just dreading having to start adding in a probably ever growing list of boolean flags that get set and unset just so that the run() method can cascade through them...- Mike
MikeKamermansa at 2007-7-12 2:41:04 > top of Java-index,Java Essentials,New To Java...