×

Loading...

Toy code for Java Thread

This morning, I discussed Thread with a friend. We cooked up some toy code. I postit here for reference in the future discussion




class MyThread extends Thread
{

public void run()
{
int x=0;
while(x<10)
{
System.out.println("x=" +x);
try{
Thread.sleep(1000);
} catch (Exception e) {}
x++;
}


}
} //end of MyThread


public class Test
{
public static void main(String[] args)
{
try
{
MyThread mt = new MyThread();
mt.start();
Thread.sleep(15000);
System.out.println("I came here");
if(mt!=null)
{
System.out.println("I 'am not null");
if(!mt.isAlive())
{
System.out.println("I am dead!");

}

mt.start();
}
else
{
System.out.println("I 'am null");
}
}
catch (Exception e)
{
e.printStackTrace();

}


}
}//end of Test
Report

Replies, comments and Discussions: