Question: Java Thread (50 points): Answer questions for the given Worker.java and ThreadExample.java. - What is the name of the class implementing the runnable interface? What

Java Thread (50 points): Answer questions for the given Worker.java and

ThreadExample.java.

- What is the name of the class implementing the runnable interface? What is the

name of the instance field of this class? (10 points)

- In which line an object of the class mentioned previous problem is created? (5

points)

- What does the join method do? (5 points)

- Give an example of output of this code (10 points)

- Are there only one possible output this code? If yes, explain why. If not, give

another possible output (10 points)

- Use your own language, describe the purpose of the code (10 points)

Java Thread (50 points): Answer questions for the given Worker.java and ThreadExample.java.

- What is the name of the class implementing the runnable interface?

Worker.java 1 public class Worker implements Runnable { private String name; public Worker (String name) { this.name = name; public void run() { 10 11 13 System.out.printf("Worker %s starts. %n", name); try { dowork(); } catch (InterruptedException e) { e.printStackTrace(); 15 16 System.out.printf("Worker %s ends.%n", name); 20 private void doWork() throws InterruptedException { int sleeptime = (int) (5 * Math.random()); Thread.sleep (sleeptime * 1000); } 24 25 26 } Thread Example.java 1 public class ThreadExample { 2 public static void main(String[] args) { Thread workerl = new Thread(new Worker("Alice")); Thread worker2 = new Thread(new Worker("Bob")); OOOO vouw System.out.println("Start worker threads"); worker1.start(); worker2.start(); try{ workeri.join(); } catch (InterruptedException e) { e.printStackTrace(); 14 16 17 18 19 try { worker2.join(); } catch (InterruptedException e) { e.printStackTrace(); 20 21 } 22 23 } 24

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!