色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

runnable能傳入參數嗎

林雅南2年前13瀏覽0評論

runnable能傳入參數嗎?

能傳入。

java中如何給Runnable線程傳遞參數? 向線程中傳遞數據的兩種方法:

一、通過構造函數傳遞參數

public class MyThread1 extends Thread

{

private String name;

public MyThread1(String name)

{

this .name = name;

}

public void run()

{

System.out.println( "hello " + name);

}

public static void main(String[] args)

{

Thread thread = new MyThread1( "world" );

thread.start();

}

}

二、通過變量和方法傳遞數據

public class MyThread2 implements Runnable

{

private String name;

public void setName(String name)

{

this .name = name;

}

public void run()

{

System.out.println( "hello " + name);

}

public static void main(String[] args)

{

MyThread2 myThread = new MyThread2();

myThread.setName( "world" );

Thread thread = new Thread(myThread);

thread.start();

}

}

java線程傳參數,runnable能傳入參數嗎