Please rewrite the two programs located below using a different approach . This
ID: 3679858 • Letter: P
Question
Please rewrite the two programs located below using a different approach. This will allow me to see how someone else may go about coding this program and ultimately coming out with the same results. Please code using Java and verify it will run in a Java IDE.
Please code TWO seperate programs, one WITH synchronization and one WITHOUT synchronization. Also please Define the Integer wrapper obect as stated in the instructions below.
Please rewrite the two programs located below using a different approach
/**The java program WithSyn that implements
* the synchronized run method that orderly
* add the sum by one for 1000 threads*/
//WithSyn.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WithSyn
{
private static Integer sum = new Integer(0);
public static void main(String[] args)
{
//Create ExecutorService of fixed number of threads
ExecutorService executor = Executors.newFixedThreadPool(1000);
for(int i = 1; i <= 1000; i++){
//call the method execute with thread object
executor.execute(new Sum(i));
}
executor.shutdown();
}
//Innner classs Sum that implements the Runnable
//interface
static class Sum implements Runnable
{
int id;
Sum(int n)
{
id = n;
}
//Override the run method that add one to sum
public synchronized void run()
{
sum=sum+1;
System.out.println("At Thread " + Thread.currentThread().getName()+
" Sum= " + sum);
}
}
}
---------------------------------------------------------------------------------------------
Smple output:
At Thread pool-1-thread-4 Sum= 1
At Thread pool-1-thread-2 Sum= 1
At Thread pool-1-thread-1 Sum= 1
At Thread pool-1-thread-6 Sum= 2
At Thread pool-1-thread-3 Sum= 3
At Thread pool-1-thread-5 Sum= 4
At Thread pool-1-thread-7 Sum= 5
At Thread pool-1-thread-9 Sum= 6
At Thread pool-1-thread-10 Sum= 7
At Thread pool-1-thread-13 Sum= 9
At Thread pool-1-thread-11 Sum= 8
At Thread pool-1-thread-8 Sum= 10
(continued)
------------------------------------------------------------------------------------------------------------------------------
/**The java program WithoutSyn that implements
* the synchronized run method that not orderly
* add the sum by one for 1000 threads*/
//WithoutSyn.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class WithoutSyn
{
private static Integer sum = new Integer(0);
public static void main(String[] args)
{
ExecutorService executor = Executors.newFixedThreadPool(1000);
for(int i = 1; i <= 1000; i++){
executor.execute(new Sum(i));
}
executor.shutdown();
}
//Innner classs Sum that implements the Runnable
//interface
static class Sum implements Runnable
{
int id;
Sum(int n)
{
id = n;
}
//The method run is not synchronized
//the sum variable takes by threads in not order
public void run()
{
sum++;
System.out.println("At Thread " + Thread.currentThread().getName()+
" Sum= " + sum);
}
}
}
Sample output:
At Thread pool-1-thread-1 Sum= 1
At Thread pool-1-thread-2 Sum= 2
At Thread pool-1-thread-5 Sum= 3
At Thread pool-1-thread-7 Sum= 4
At Thread pool-1-thread-4 Sum= 5
At Thread pool-1-thread-12 Sum= 12
At Thread pool-1-thread-9 Sum= 11
At Thread pool-1-thread-11 Sum= 11
At Thread pool-1-thread-10 Sum= 10
Explanation / Answer
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
public class WithSyn
{
Integer sum = new Integer(0) ;
withSyn() {
ExecutorService executor = Executors.newFixedThreadPool ( 1000 ) ;
Sum add = new Sum() ;
for ( int i = 0 ; i < 1000 ; i++ ) {
executor.execute ( add ) ;
} executor.shutdown ( ) ;
while ( !executor.isTerminated ( ) ) {
}
System.out.println ( sum ) ;
}
public static void main ( String [] args ) {
new withSyn ( ) ;
}
class Sum implements Runnable {
@Override
public void run ( ) {
synch ( ) ;
}
public synchronized void synch ( ) {
sum = sum + 1 ;
System.out.println("At Thread " + Thread.currentThread().getName()+ " Sum= " + sum);
}
}
}
import java.util.concurrent.ExecutorService ;
import java.util.concurrent.Executors ;
public class WithoutSyn
{
Integer sum = new Integer(0) ;
withSyn() {
ExecutorService executor = Executors.newFixedThreadPool ( 1000 ) ;
Sum add = new Sum() ;
for ( int i = 0 ; i < 1000 ; i++ ) {
executor.execute ( add ) ;
} executor.shutdown ( ) ;
while ( !executor.isTerminated ( ) ) {
}
System.out.println ( sum ) ;
}
public static void main ( String [] args ) {
new withSyn ( ) ;
}
class Sum implements Runnable {
@Override
public void run ( ) {
notSynch ( ) ;
}
public void notSynch ( ) {
sum = sum + 1 ;
System.out.println("At Thread " + Thread.currentThread().getName()+ " Sum= " + sum);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.