Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image text in transcribed

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 23rd European Conference Adbis 2019 Bled Slovenia September 8 11 2019 Proceedings Lncs 11695

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Aida Kamisalic Latific

1st Edition

3030287297, 978-3030287290

More Books

Students also viewed these Databases questions

Question

=+Describe the perceptual signs of voice and resonance disorders

Answered: 1 week ago