close
1. class NameRunnable
package Threadnew;
import Threadnew.NameRunnable;
class NameRunnable implements Runnable
{
public void run()
{
for(int x=1;x<=3;x++)
{
System.out.println("Run by"+Thread.currentThread().getName()+",x is"+x);
}
}
}
2. class Name
package Threadnew;
public class Name
{
public static void main(String[] args)
{
NameRunnable nr=new NameRunnable();
Thread one =new Thread(nr);
Thread two =new Thread(nr);
Thread three=new Thread(nr);
one.setName("Niki");
two.setName("Love");
three.setName("Tea");
one.start();
two.start();
three.start();
}
}
3 結果
Run byLove,x is1
Run byTea,x is1
Run byNiki,x is1
Run byTea,x is2
Run byLove,x is2
Run byTea,x is3
Run byNiki,x is2
Run byNiki,x is3
Run byLove,x is3
全站熱搜