1 assume shared variable x initially 0 and threads t1 and t2 are follows

1. Assume that shared variable x is initially 0 and that threads T1 and T2 are as follows: T1 T2 x = x + 1; x = x – 1; What are the possible final values of x? (The statements are not inside critical sections.) Show your work. 2. Write a Java program with four threads. Your program should work as follows: – Each thread performs a simple task which is to output its thread ID, increment a shared variable named count, and display the resulting value of count. Count is initalized to 0. – The threads work in pairs. Threads 1 and 2 work as a pair, and Threads 3 and 4 work as a pair. – The threads in each pair alternate peforming their task. That is, Thread1 performs its task first, then Thread2, then Thread1, then Thread2, etc. Same for Threads 3 and 4 with Thread 3 going first. – The thread pairs also alternate. That is, Thread pair 1 and 2 perform their tasks then Thread pair 3 and 4 perform theirs, then Thread1 and Thread2, then Thread3 and Thread4, etc. – The user enters on the command line the number of times that the threads should alternate. For example, if the user enters 2, each thread in each pair performs its work twice, and each thread pair is given 2 chances to execute. – At then end of execution, display the final value of shared variable count. You must use counting semaphores to force your threads to alternate. Sample output for: java Alternate 2 T1:1 // each of T1 and T2 performs its task 2 times, with T1 and T2 alternating T2:2 T1:3 T2:4 T3:5 // then each of T3 and T4 performs its task 2 times, with T3 and T4 alternating T4:6 T3:7 T4:8 T1:9 // then each of T1 and T2 performs its task 2 times, with T1 and T2 alternating T2:10 T1:11 T2:12 T3:13 // then each of T3 and T4 performs its task 2 times, with T3 and T4 alternating T4:14 T3:15 T4:16 count is 16 Sample output for: java Alternate 3 T1:1 T2:2 T1:3 // each of T1 and T2 performs its task 3 times, with T1 and T2 alternating T2:4 T1:5 T2:6 T3:7 // then each of T3 and T4 performs its task 3 times, with T3 and T4 alternating T4:8 T3:9 T4:10 T3:11 T4:12 T1:13 // a 2nd time for pair T1 and T2 T2:14 T1:15 T2:16 T1:17 T2:18 T3:19 // a 2nd time for pair T3 and T4 T4:20 T3:21 T4:22 T3:23 T4:24 T1:25 // a 3rd time for pair T1 and T2 T2:26 T1:27 T2:28 T1:29 T2:30 T3:31 // a 3rd time for pair T3 and T4 T4:32 T3:33 T4:34 T3:35 T4:36 count is 36 I created a template for you in file Alternate.java (attached). Complete this code by completing the run() methods for the threads and defining the necessary semaphores. The Java countingSemaphore and TDThread classes are in a Java jar file, which can be found in the following zip file: http://www.cs.gmu.edu/~rcarver/ModernMultithreading/ModernMultithreadingJavaJar.zip Class countingSemaphore has operations P() and V() (instead of the acquire() and release() operations, respectively, used in textbook.) The Zip file also contains some directories with sample programs; see the directories with “Semaphores” in their names. Assuming the jar file is in the same directory as your Alternate.java file: Compile your program using: Windows: javac -classpath .;ModernMultithreading.jar Alternate.java Unix: javac -classpath .:ModernMultithreading.jar Alternate.java Run your program using: Windows: java -classpath .;ModernMultithreading.jar Alternate 2 // choose 2 or some other number of iterations Unix: java -classpath .:ModernMultithreading.jar Alternate 2 If you are having deadlock problems, it may help to run your program with deadlock detection turned on: Windows: java -classpath .;ModernMultithreading.jar -Dmode=trace -DdeadlockDetection=on Alternate 2 Unix: java -classpath .:ModernMultithreading.jar -Dmode=trace -DdeadlockDetection=on Alternate 2 Deadlock detection will provide helpful debugging information about the cuase of the deadlock. To make this information more readable, it helps to name your semaphores when you create them: countingSemaphore mutex = new countingSemaphore(1,”mutex”); // initial value is 1, name is “mutex” The name you supply will be used to identify your semaphore in the debug messages.
Looking for solution of this Assignment?


We deliver quality original papers

Our experts write quality original papers using academic databases.  

Free revisions

We offer our clients multiple free revisions just to ensure you get what you want.

Discounted prices

All our prices are discounted which makes it affordable to you. Use code FIRST15 to get your discount

100% originality

We deliver papers that are written from scratch to deliver 100% originality. Our papers are free from plagiarism and NO similarity

On-time delivery

We will deliver your paper on time even on short notice or  short deadline, overnight essay or even an urgent essay