We discussed in class many different ways to create, control, and synchronize co
ID: 3696162 • Letter: W
Question
We discussed in class many different ways to create, control, and synchronize concurrent processes. Of course, there are even more... In this exercise you implement a common cooperation pattern using at least 3 different concepts from at least 2 different programming languages. You can use processes with System V interprocess communication pthreads C++11 threads protothreads Erlang actors Boost coroutines CO2 coroutines Scala Actor class Java synchronized methods Go goroutines any of the concurrency models offered in Haskell (many available!) Clojure (also offers several different approaches) You can use any one of these programming languages, libraries, concepts to implement the producer-consumer pattern. Create a relatively small buffer and a very much larger number of interactions between producer and consumer (e.g. buffer size 25 and 100000000 items to process). Make sure your cooperation is correct: no items are consumed more than once, no item is consumed before it is produced. 110 unconsumed item is overwritten by a freshly produced one.Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package synchronize1;
/**
*
* @author Admin
*/
public class our2ndThread extends Thread {
static Synchronize1.Stack s;
our2ndThread(Synchronize1.Stack s) {
our2ndThread.s = s; // this
}
public void execute() {
s.displayStack(90);
} // end func execute
} // end class our2ndThread
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package synchronize1;
/**
*
* @author Admin
*/
public class our1stThread extends Thread{
static Synchronize1.Stack s;
private our1stThread(Synchronize1.Stack sOne) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
void ourThread(Synchronize1.Stack s) {
our1stThread.s = s;
} // end function ourThread(Stack s) not the class
public void execute() {
s.displayStack(10);
} // end func execute
} // end class our1stThread
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package synchronize1;
/**
*
* @author Admin
*/
public class Synchronize1 {
// let us make a synchronized method:
class Stack {
synchronized void displayStack (int nn) {
for (int ii = 1; ii<= 10; ii++)
System.out.println(nn*ii);
try{ Thread.sleep(250); }
catch (Exception errStk ) { System.out.println(errStk); }
} // end disp stk
} // end class stk
//public class checkSynchronizedOKOrNotOK {
//} // end class check sync ok or not ok
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Stack sOne = new Stack();
our1stThread ot1st;
ot1st = new our1stThread(sOne);
our2ndThread ot2nd;
ot2nd = new our2ndThread(sOne);
ot1st.start();
ot2nd.start();
} // end main
} // end public class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.