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)
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 } 24Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started