/**
* @param args
*/
public static void main(String[] args)
{
// TODO 自动生成方法存根
TxtThread tt = new TxtThread();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
new Thread(tt).start();
}
}
class TxtThread implements Runnable
{
int num = 100;
String str = new String();
public void run()
{
while (true)
{
synchronized(str)
{
if (num>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
e.getMessage();
}
System.out.println(Thread.currentThread().getName()+ "this is "+ num--);
}
}
}
}
}
class TestThread{
public static void main(String args []){
myRunnable rn = new myRunnable();
Thread thread1 = new Thread(rn);
Thread thread2 = new Thread(rn);
thread1.setName("线程A");
thread2.setName("线程B");
thread1.start();
thread2.start();
}
}
class myRunnable implements Runnable{
public void run(){
synchronized (this){
for(int i=0;i<100;i++)
System.out.println(Thread.currentThread().getName()+i);
}