Reputation: 143
Its not homework.Part of some practice am doing.Here they asked me to run 2 threads doing different things but I don't know if we can run 2 run() methods.How can I perform 2 different operations? Sorry am just starting to learn threads.This is what I've done but it is incomplete.
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();
}
}
Upvotes: 1
Views: 652
Reputation: 2260
As mentioned by @dicarlo2, a BlockingQueue would also solve your problem. But I think you are going about this question in the wrong manner. Here is my multi-threaded solution. I have added a System.out.println statement in the ShowMark class so that we know that the magic is working. You will also see the students printed out of order, every now and then. I hope this helps!
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);
}
}
}
Upvotes: 1
Reputation: 136062
Change it as
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();
}
}
Besides, I suggest to make 2 separate classes - Student and MarkEvaluationThread, Student implements Runnable
looks awful
Upvotes: 1
Reputation: 4891
You're on the right track. What you want to do now is create another Runnable
class for displaying the resulting sum, and you need to find some way of sharing the calculated sum from the MarkEvaluation
thread. I suggest using a BlockingQueue
, as the take()
method will block until the sum
has been put
onto the thread, satisfying your last requirement without having to use join()
.
Explicitly, you need 2 classes implementing Runnable
, 1 you already have, and another for displaying the result. You need to share a BlockingQueue
between them, and you need to start both threads by instantiating a Thread
and calling start()
. Hopefully this will put you on the right track without simply giving you the code.
Alternatively, you could have the ShowMark
Runnable
keep a reference to Student
as well as the thread used to run Student
(called MarkEvaluation
above), join
on the thread, and then call getSum()
(or similar) to retrieve the sum from Student
.
Upvotes: 1