Java 线程生命周期 使用name、mark1、mark2、mark3和sum变量创建一个学生类 创建一个包含4个具有某些值的学生对象的ArrayList 创建MarkEvaluation线程和ShowMark线程 分数评估应计算每个学生的总分数 ShowMark线程应显示计算的总和 使用join方法防止在MarkEvaluation之前运行ShowMark线程
这不是家庭作业。我在做一些练习。这里他们让我运行两个线程做不同的事情,但我不知道我们是否可以运行两个run()方法。我如何执行两个不同的操作?对不起,我刚刚开始学习线程。这是我所做的,但它是不完整的Java 线程生命周期 使用name、mark1、mark2、mark3和sum变量创建一个学生类 创建一个包含4个具有某些值的学生对象的ArrayList 创建MarkEvaluation线程和ShowMark线程 分数评估应计算每个学生的总分数 ShowMark线程应显示计算的总和 使用join方法防止在MarkEvaluation之前运行ShowMark线程,java,eclipse,multithreading,Java,Eclipse,Multithreading,这不是家庭作业。我在做一些练习。这里他们让我运行两个线程做不同的事情,但我不知道我们是否可以运行两个run()方法。我如何执行两个不同的操作?对不起,我刚刚开始学习线程。这是我所做的,但它是不完整的 import java.util.*; class Student implements Runnable { public Student() { List<Object> list = new ArrayList<Object>(); list
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();
}
}
import java.util.*;
班级学生实现Runnable
{
公立学生()
{
列表=新的ArrayList();
列表。添加(“robin”);
列表。添加(“ravi”);
列表。添加(“raj”);
列表。添加(“sam”);
}
字符串名;
int mark1=30,mark2=45,mark3=70,sum=0;
公开募捐
{
总和=mark1+mark2+mark3;
}
}
公共类Ch3Lu2Ex3
{
公共静态void main(字符串[]args)
{
学生stu=新学生();
螺纹标记评估=新螺纹(stu);
MarkEvaluation.start();
螺纹显示标记=新螺纹();
}
}
你走对了方向。您现在要做的是创建另一个Runnable
类来显示结果和,您需要找到某种方法来共享MarkEvaluation
线程中计算出的和。我建议使用,因为take()
方法将阻塞,直到sum
被put
放到线程上,满足您的最后一个要求,而不必使用join()
明确地说,您需要两个类来实现Runnable
,一个已经实现,另一个用于显示结果。您需要在它们之间共享一个BlockingQueue
,并且您需要通过实例化Thread
并调用start()
来启动这两个线程。希望这能让你走上正轨,而不只是简单地给你代码
或者,您可以让ShowMark
Runnable
保留对Student
的引用,以及用于运行Student
的线程(上面称为MarkEvaluation
),在线程上加入join
,然后调用getSum()
(或类似)要从学生
中检索总和,请将其更改为
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();
}
}
此外,我建议创建两个单独的类-Student和MarkEvaluationThread,Student implements Runnable
看起来很糟糕正如@dicarlo2所提到的,阻塞队列也可以解决您的问题。但我认为你在这个问题上的态度是错误的。这是我的多线程解决方案。我在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);
}
}
}
import java.util.ArrayList;
导入java.util.List;
公共班机{
公共静态void main(字符串[]args){
List students=new ArrayList();
添加(新学生(“Alice”,10,20,30));
添加(新学生(“Bob”,20,30,40));
添加(新学生(“卡罗尔”,30、40、50));
添加(新学生(“罗宾”,40,50,60));
(学生:学生){
线程mkThread=新的标记评估;
mkThread.start();
//必须在启动ShowMark之前至少启动线程。
螺纹smThread=新的显示标记(s,mkThread);
smThread.start();
}
}
私立静态班学生{
字符串名;
双马克1,马克2,马克3,和;
公立学生(字符串名称,双标记1,双标记2,双标记3){
this.name=名称;
this.mark1=mark1;
this.mark2=mark2;
this.mark3=mark3;
}
}
私有静态类MarkEvaluation扩展了线程{
学生斯图;
公众评价(学生stu){
this.stu=stu;
}
@凌驾
公开募捐{
super.run();
stu.sum=stu.mark1+stu.mark2+stu.mark3;
}
}
私有静态类ShowMark扩展线程{
螺纹;
学生斯图;
公共展示标志(学生stu,螺纹){
this.stu=stu;
this.thread=线程;
}
@凌驾
公开募捐{
super.run();
//如果线程已启动但尚未死亡,则该线程是活动的。
while(thread.isAlive()){
试一试{
System.out.println(“等待线程:+thread.toString()+”完成”);
thread.join();
}捕捉(中断异常e){
e、 printStackTrace();
}
}
System.out.println(stu.name+“”+stu.sum);
}
}
}
哦,谢谢……我会调查一下:)但他们让我使用加入……我该如何使用它?@Robin更新了我的帖子。另外,为了将来的参考,欢迎你通过合理的尝试(正如你所做的那样)在SO上发布家庭作业问题。非常感谢:)我会尝试你告诉我的一切:)非常感谢……我从你的方式正确地理解了:)我只是有一些事情要指出/评论。你的目标说明了一些事情,而你似乎在做不同的事情。1.是的,你做得对。注意:学生是否必须实现Runnable/学生是否应该实现Runnable?2.在学生类中创建字符串列表。这就是你想要的吗?你不想要ArrayList吗?学生不应该被创造出来吗?3.MarkEvaluation线程可能应该在其构造函数中包含一个学生,并计算一个特定学生的所有分数之和(确实如此,但您的学生构造函数令人困惑)。我正在编写一个解决方案,说明我认为应该如何做。我想你已经把很多东西混在一起了。非常感谢你:D你的方法很清楚……我明白了:)我只是还没有学到这么多,以至于我能做到这一切