public class multithread
{
public static void main(String[] args)
{
MyThread t1 = new MyThread("First");
MyThread t2 = new MyThread("Second");
new Thread(t1).start();
new Thread(t2).start();
}
}
class MyThread implements Runnable
{
String id;
int counter=0;
public boolean stopFlag;
multithread instance;
MyThread(String id)
{
this.id=id;
}
public void run()
{
stopFlag=false;
System.out.println(id+"start:");
while(!stopFlag)
{
// synchronized(counter)
{
System.out.println(id+":"+counter++);
try
{
Thread.sleep((int)(1000*Math.random()));
}
catch(Exception e){}
if(counter>20)stopFlag=true;
}
}
}
}