[英]Understanding Threads
我試圖找出如何從我的公共靜態void main運行NamedRunnable。 我實質上是在嘗試兩種方法來運行線程,第一種是創建和定義線程,第二種是定義類,然后實現可運行。
這是我的代碼
package threads;
public class Threads extends Thread{
private final String name; // The name of this thread
public static void main(String[] args) {
long lStartTime = System.nanoTime();
Threads greetings = new Threads("Fred");
Threads greetings1 = new Threads("Betty");
NamedRunnable greetings2 = new NamedRunnable("Ralph");//it is here i cant seem to create an instance of Named Runnable and therefore call start
greetings.start();
greetings1.start();
greetings2.start();
long lEndTime = System.nanoTime();
long difference = lEndTime - lStartTime;
System.out.println("Elapsed time: " + difference);
}
public Threads(String name) {
this.name = name;
}
public void run() { // The run method prints a message to standard output.
System.out.println("Greetings from thread ’" + name + "’!");
}
public class NamedRunnable implements Runnable {
private final String name;
// The name of this Runnable.
public NamedRunnable(String name) { // Constructor gives name to object.
this.name = name; }
public void run() { // The run method prints a message to standard output.
System.out.println("Greetings from runnable ’" + name +"’!"); } }
}
線程和Runnable是2種不同的東西:
線程是映射到OS線程的對象。 在線程上調用start分配並執行一個線程。
Runnable描述要執行的任務。
線程只是執行Runnable的一種方法。 您可以使用Thread運行Runnable,如
Runnable myRunnable = new Runnable() {
public void run() {
System.out.println("Hello");
}
};
new Thread(myRunnable).start();
或者您可以將Runnable提交給ExecutorService並讓服務決定如何執行它:
executorService.submit(myRunnable);
或者您可以在當前線程中執行Runnable:
myRunnable.run();
為了方便起見,有人決定讓Thread實現Runnable,因此他們可以用更少的代碼編寫演示。
將Runnable
傳遞給Thread
以運行它,或使用另一個類(如ExecutorService
。
new Thread( greetings2 ).start();
順便說一句,這可能是一個可怕的想法:
public class Threads extends Thread{
//...
public void run() { // The run method prints a message to standard output.
System.out.println("Greetings from thread ’" + name + "’!");
}
諸如run()
和start()
類的重載方法可能導致各種混亂。 執行此操作時,您將完全更改其語義(即,它們的作用)。 使用Thread
而不對其進行子類化,或使用ExecutorService
類的類。 子類化Thread
就像上個世紀一樣。
Thread類的構造函數之一接受Runnable實例,如下所示:
public Thread(Runnable target)
分配一個新的Thread對象。 此構造函數與Thread(空,目標,gname)具有相同的作用,其中gname是新生成的名稱。 自動生成的名稱的形式為“ Thread-” + n,其中n為整數。
參數:target-啟動此線程時將調用run方法的對象。 如果為null,則此類run方法不執行任何操作。
所以這樣做:
Thread t = new Thread(greetings2);
t.start();
當線程啟動時,它將踢您的NamedRunnable.run
方法。
我認為您可以使用ExexutorService來實現函數的相關性,上周,我讀了一些書,因為Java 1.5是最好的答案。
public class LiftOff implements Runnable {
protected int countDown = 10;
private static int taskCount = 0;
private final id = taskCount++;
public LiftOff(){}
public LiftOff(int counDown) {
this.countDown = countDown;
}
public String status() {
return "#" + id + "(" + (countDown >0 ? countDown : "LiftOff!") + ")";
}
public void run() {
while(countDown-- > 0) {
System.out.pritln(status);
Thread.yield();
}
}
}
public class Test {
public static void main(Strig args[]) {
ExectorService execu = Exector.newCachedThreadPool();
for(int i = 0; i++) {
execu.execute(new LiftOff());
execu.shutdown();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.