2013-01-13 93 views
1
  1. 用name,mark1,mark2,mark3和sum變量創建一個Student類。
  2. 用4個具有一些值的學生對象創建一個ArrayList。
  3. 創建MarkEvaluation線程和ShowMark線程。
  4. MarkEvaluation應執行每個學生總分的計算。
  5. ShowMark線程應顯示計算的總和。
  6. 使用連接方法可防止ShowMark線程在MarkEvaluation之前運行。

它不是家庭作業。部分練習正在做。他們要求我運行2個線程做不同的事情,但我不知道我們是否可以運行2個run()方法。如何執行2個不同的方法操作?對不起剛剛開始學習線程。這是我所做的,但它不完整。線程的生命週期

import java.util.*; 

class Student implements Runnable 
{ 
public Student() 
{ 
    List<Object> list = new ArrayList<Object>(); 
    list.add("robin"); 
    list.add("ravi"); 
    list.add("raj"); 
    list.add("sam"); 
} 
String name; 
int mark1=30,mark2=45,mark3=70,sum=0; 

public void run() 
    { 
    sum = mark1+mark2+mark3; 
    } 

} 

public class Ch3Lu2Ex3 
{ 
    public static void main(String[] args) 
    { 
    Student stu = new Student(); 
    Thread MarkEvaluation = new Thread(stu); 
    MarkEvaluation.start(); 
    Thread ShowMark = new Thread(); 

} 
} 
+0

我只是有些事情要指出/評論。你的目標說一些事情,你似乎做了不同的事情。 1.是的,你做得對。注意:學生是否必須實現Runnable/should學生是否實現Runnable? 2.您在學生課堂內創建一個STRINGS列表。那是你要的嗎?你不想要ArrayList ?學生不應該創建主要? 3. MarkEvaluation線程應該在其構造函數中接受一個Student並計算特定學生的所有分數的總和(它所做的但是您的學生構造函數令人困惑)。 – Sanchit

+0

我正在寫一個關於我應該怎麼做的解決方案。我認爲你們混合了很多東西。 – Sanchit

回答

1

你是在正確的軌道上。你現在想要做的是創建另一個Runnable類來顯示結果總和,並且您需要找到某種方法來共享MarkEvaluation線程中計算出的總和。我建議使用BlockingQueue,因爲take()方法會阻塞,直到sum已被put轉到線程上,滿足您的最後一個要求,而不必使用join()

明確地說,您需要2個類來實現Runnable,1已經有了,另一個用於顯示結果。您需要在它們之間共享一個BlockingQueue,並且您需要通過實例化Thread並呼叫start()來啓動這兩個線程。希望這會讓你走上正確的軌道,而不是簡單地給你代碼。

或者,你可以有ShowMarkRunnable保持到Student參考以及用於運行Student線程(以上稱爲MarkEvaluation),join的線程上,然後調用getSum()(或類似)來檢索和Student

+0

哦,謝謝......我會研究它:)但他們要求我使用加入...我該如何使用它? – Robin

+1

@Robin更新了我的文章。另外,爲了將來的參考,歡迎您通過合理的嘗試(就像您所做的那樣)在SO上發佈作業問題。 –

+0

非常感謝你:)我會嘗試所有你告訴我的:) – Robin

1

更改它作爲

class Student implements Runnable { 
... 
} 

class ShowMarkThread extends Thread { 
    private final int sum; 

    ShowMarkThread(int sum) { 
     this.sum = sum; 
    } 

    @Override 
    public void run() { 
     System.out.println(sum); 
    } 
} 

public class Ch3Lu2Ex3 { 

    public static void main(String[] args) throws Exception { 
     Student stu = new Student(); 
     Thread markEvaluation = new Thread(stu); 
     markEvaluation.start(); 
     markEvaluation.join(); 
     new ShowMarkThread(stu.sum).start(); 
    } 
} 

此外,我建議做2單獨的類 - 學生和MarkEvaluationThread,Student implements Runnable看起來可怕

+0

非常感謝...我從你的方式正確理解:) – Robin

1

正如@ dicarlo2提到的,BlockingQueue的也能解決你的問題。但我認爲你是以錯誤的方式回答這個問題的。這是我的多線程解決方案。我已經在ShowMark類中添加了一個System.out.println語句,以便我們知道這個魔法正在發揮作用。您還會看到學生們不時打印的問題。我希望這有幫助!

import java.util.ArrayList; 
import java.util.List; 


public class Main { 

    public static void main(String[] args) { 
    List<Student> students = new ArrayList<Student>(); 
    students.add(new Student("Alice", 10, 20, 30)); 
    students.add(new Student("Bob", 20, 30, 40)); 
    students.add(new Student("Carol", 30, 40, 50)); 
    students.add(new Student("Robin", 40, 50, 60)); 
    for (Student s : students) { 
     Thread mkThread = new MarkEvaluation(s); 
     mkThread.start(); 
     //Must at least start thread before starting ShowMark. 
     Thread smThread = new ShowMark(s, mkThread); 
     smThread.start(); 
    } 
    } 

    private static class Student { 
    String name; 
    double mark1, mark2, mark3, sum; 

    public Student(String name, double mark1, double mark2, double mark3) { 
     this.name = name; 
     this.mark1 = mark1; 
     this.mark2 = mark2; 
     this.mark3 = mark3; 
    } 
    } 

    private static class MarkEvaluation extends Thread { 
    Student stu; 
    public MarkEvaluation(Student stu) { 
     this.stu = stu; 
    } 
    @Override 
    public void run() { 
     super.run(); 
     stu.sum = stu.mark1 + stu.mark2 + stu.mark3; 
    } 
    } 

    private static class ShowMark extends Thread { 
    Thread thread; 
    Student stu; 
    public ShowMark(Student stu, Thread thread) { 
     this.stu = stu; 
     this.thread = thread; 
    } 
    @Override 
    public void run() { 
     super.run(); 
     // A thread is alive if it has been started and has not yet died. 
     while (thread.isAlive()) { 
     try { 
      System.out.println("Waited for thread: " + thread.toString() + " to finish."); 
      thread.join(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     } 
     System.out.println(stu.name + " " + stu.sum); 
    } 
    } 

} 
+0

非常感謝:D你的方式非常清楚...我得到了它:)我只是沒有學到這麼多,以便能夠做到這一切......現在我將學習......謝謝:) – Robin