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


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 布拉怡 的頭像
    布拉怡

    nini的部落格

    布拉怡 發表在 痞客邦 留言(0) 人氣()