Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the following attached file , if you run the code you will find an output similar to this [ Syncronized [ World ] [

In the following attached file , if you run the code you will find an output similar to this

[ Syncronized [ World ] [ Hello ] ] or [ World [ Hello ] [ Syncronized ] ]

Please use synchronization to make the output looks like this:

[ Hello ] [ Syncronized ] [ World ]

or

[ Hello ] [ World ] [ Syncronized ]

where [ should be before the word and ] should be after the word.

No matter the order of the words is.

you can use either, Lock..Semaphore, Or Syncronized block or methods

image text in transcribed

21 1 public class NonSynchronized { 2 30 public static void main(String args[]) { 4 MsgSender sender = new MsgSender(); 5 Caller c1, c2,c3; 6 c1 = new Caller("Hello", sender); 7 c2 = new Caller("World", sender); 8 c3 = new Caller("Syncronized", sender); 9 Thread t1 = new Thread(c1); 10 Thread t2 = new Thread(c2); 11 Thread t3 = new Thread(c3); 12 13 t1.start(); 14 t2.start(); 15 t3.start(); 16 try { 17 t1.join(); 18 t2.join(); 19 t3.join(); 20 catch(InterruptedException ie) { 22 } 23 } 24 } 25 class MsgSender{ 26- void sendMsg(String msg) { System.out.print(" [" + msg ); 28 System.out.print(" ] "); 29 } 30 31 class Caller implements Runnable{ 32 String msg; 33 MsgSender sender; 34 35- public Caller(String msg ,MsgSender sender) { 36 this.msg = msg; 37 this.sender = sender; 38 public void run() { 40 sender.sendMsg(msg); 41 42] 43 | 27

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

Step: 3

blur-text-image

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

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions