The above examples have to exactly match the output to be generated as it is sho
ID: 3577149 • Letter: T
Question
The above examples have to exactly match the output to be generated as it is shown.
2. Create a class named SharedResults as follows. The class keeps track of the shared results. e. The instance (or member) private variable results (ArrayList of integers). f. A default constructor that initializes the above data structure. g. A void addToResults method which takes two arguments, the calling thread's turn and the contributing result that needs to be added to the shared results. Implement the wait and notifyAll functionality in this method. Print to the console the thread's turn, the name of the current thread, the value it added, and the shared results data structure. Handle the synchronization issue with this method. Use the size of the data structure to determine if it is the calling thread's turn. h. The getResult method with no arguments which returns the sum of the values in the shared results data structure. Handle the synchronization issue with this method. 4. Create a class named LongTask which extends the Thread class. a. The instance (or member) private variables sharedData (of type SharedResults), start (integer) end (integer) and tunn (integer). b. A single constructor which takes the above four arguments and stores them in the instance values. Also, create a name for this thread as Thread start send> c. In the run method, add the integer numbers from start to end (both inclusive) using a for loop. Also, sleep for a random time (up to 10 seconds in each iteration of the loop. After the loop, invoke the addToResults method of the shared object and provide this thread's turn and its this accumulated sum.Explanation / Answer
makeResult = generateTheResult(); // Not synchronized!
synchronized(lock) {
sharedResult = makeResult;
lock.notify();
}
synchronized(lock) {
while ( sharedResult == null ) {
try {
lock.wait();
}
catch (InterruptedException e) {
}
}
useResult = sharedResult;
}
useTheResult(useResult);
import java.util.LinkedList;
public class MyLinkedBlockingQueue {
private LinkedList<Runnable> taskList = new LinkedList<Runnable>();
public void clear() {
synchronized(taskList) {
taskList.clear();
}
}
public void add(Runnable task) {
synchronized(taskList) {
taskList.addLast(task);
taskList.notify();
}
}
public Runnable take() throws InterruptedException {
synchronized(taskList) {
while (taskList.isEmpty())
taskList.wait();
return taskList.removeFirst();
}
}
}
public class ThreadJoinTest_01 {
private boolean joinTask = true;
public static void main( String[] args ) {
new ThreadJoinTest_01();
}
public ThreadJoinTest_01() {
LongTask task = new LongTask();
task.start();
if( joinTask ) {
try {
task.join();
} catch( InterruptedException e ) {
e.printStackTrace();
}
}
System.out.println( "Main thread complete" );
}
}
class LongTask extends Thread {
private boolean joinSecond = true;
public void run() {
System.out.println( "Starting LongTask" );
try {
sleep( 2000 );
SecondTask second = new SecondTask();
second.start();
if( joinSecond ) {
second.join();
}
} catch( InterruptedException e ) {
e.printStackTrace();
}
System.out.println( "LongTask complete" );
}
}
class SecondTask extends Thread {
public void run() {
System.out.println( "Starting SecondTask" );
try {
sleep( 2000 );
} catch( InterruptedException e ) {
e.printStackTrace();
}
System.out.println( "SecondTask complete" );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.