problem: Multithreading calculation 1+2+...+100, such as: up to four threads, calculate 1+2+ respectively. 25, 26+27+...+50, 51+52+...+75, 76+77+ ... 100, finally the sum is related, the output should be 5050
Workaround: Call Thread.Join () in turn, and the main thread outputs the result. Note: Sum is a shared variable, when you access a shared variable, use synchronized to synchronize the Countdownlatch, and the child thread finishes calling Countdownlatch.countdown (). The main thread calls Countdownlatc.await () to wait for the child thread to execute and output the result. Note: Sum is a shared variable, when you access a shared variable, you use synchronized to synchronize the cyclicbarrier, the child thread finishes the call to Cyclicbarrier.await (), and when you finally reach barrier, the output results. Note: Sum is a shared variable, accessing shared variables, using synchronized synchronization to manage threads through the thread pool, using future to obtain the results of each child thread execution, and finally adding the result. using Thread.Join
Call Thread.Join () in turn, and the main thread outputs the results. Note: Sum is a shared variable that is synchronized with synchronized when accessing the shared variable. The code is as follows:
Package thread;
public class Threadadd {public static int sum = 0;
public static Object LOCK = new Object ();
public static void Main (string[] args) throws Interruptedexception {threadadd add = new Threadadd ();
ThreadTest thread1 = Add.new threadtest (1, 25);
ThreadTest thread2 = Add.new ThreadTest (26, 50);
ThreadTest thread3 = Add.new threadtest (51, 75);
ThreadTest thread4 = Add.new threadtest (76, 100);
Thread1.start ();
Thread2.start ();
Thread3.start ();
Thread4.start ();
Thread1.join ();
Thread2.join ();
Thread3.join ();
Thread4.join ();
SYSTEM.OUT.PRINTLN ("Total result:" +sum);
Class ThreadTest extends Thread {private int begin;
private int end; @Override public void Run () {synchronized (LOCK) {to (int i = begin; I <= end; I
+ +) {sum = i;
} System.out.println ("from" +thread.currentthread (). GetName () + "sum=" +sum);
} public threadtest (int begin, int end) {this.begin = begin;
This.end = end;
}
}
}
Output:
From Thread-0 sum=325
From Thread-1 sum=1275
From Thread-2 sum=2850
From Thread-3 sum=5050
Total result:5050 use Countdownlatch
After the child thread completes the call Countdownlatch.countdown (), the main thread calls Countdownlatc.await () to wait for the child thread to execute and output the result. Note: Sum is a shared variable, and when you access the shared variable, synchronize with synchronized with the following code:
Package thread;
Import Java.util.concurrent.CountDownLatch;
public class Threadaddlatch {public static int sum = 0;
public static Object LOCK = new Object ();
public static Countdownlatch countdown = new Countdownlatch (4);
public static void Main (string[] args) throws Interruptedexception {Threadaddlatch add = new Threadaddlatch ();
ThreadTest thread1 = Add.new threadtest (1, 25);
ThreadTest thread2 = Add.new ThreadTest (26, 50);
ThreadTest thread3 = Add.new threadtest (51, 75);
ThreadTest thread4 = Add.new threadtest (76, 100);
Thread1.start ();
Thread2.start ();
Thread3.start ();
Thread4.start ();
Countdown.await ();
SYSTEM.OUT.PRINTLN ("Total result:" +sum);
Class ThreadTest extends Thread {private int begin;
private int end; @Override public void Run () {synchronized (LOCK) {to (int i = begin; I <= end; I ++){sum = i;
} System.out.println ("from" +thread.currentthread (). GetName () + "sum=" +sum);
} countdown.countdown ();
ThreadTest (int begin, int end) {this.begin = begin;
This.end = end;
}
}
}
Output:
From Thread-0 sum=325
From Thread-3 sum=2525
From Thread-2 sum=4100
From Thread-1 sum=5050
Total result:5050 use Cyclicbarrier
When the child thread finishes calling Cyclicbarrier.await (), and finally arrives at barrier, the output results. Note: Sum is a shared variable, and when you access the shared variable, synchronize with synchronized with the following code:
Package thread;
Import java.util.concurrent.BrokenBarrierException;
Import Java.util.concurrent.CyclicBarrier;
public class Threadaddbarrier {public static int sum = 0;
public static Object LOCK = new Object (); public static Cyclicbarrier Cyclicbarrier = new Cyclicbarrier (4, New Runnable () {public void
Run () {System.out.println (sum);
}
}); public static void Main (string[] args) throws Interruptedexception {threadaddbarrier add = new Threadaddbarrier ()
;
ThreadTest thread1 = Add.new threadtest (1, 25);
ThreadTest thread2 = Add.new ThreadTest (26, 50);
ThreadTest thread3 = Add.new threadtest (51, 75);
ThreadTest thread4 = Add.new threadtest (76, 100);
Thread1.start ();
Thread2.start ();
Thread3.start ();
Thread4.start ();
Class ThreadTest extends Thread {private int begin;
private int end;@Override public void Run () {synchronized (LOCK) {to (int i = begin; I <= end; I
+ +) {sum = i;
} System.out.println ("from" +thread.currentthread (). GetName () + "sum=" +sum);
try {cyclicbarrier.await (); catch (Interruptedexception |
Brokenbarrierexception e) {e.printstacktrace ();
} public threadtest (int begin, int end) {this.begin = begin;
This.end = end;
}
}
}
Output:
From Thread-0 sum=325
From Thread-2 sum=1900
From Thread-1 sum=2850
From Thread-3 sum=5050
5050 manages threads through the thread pool
Using future to get the results of each child thread, the result is added. The code is as follows:
Package thread;
Import java.util.ArrayList;
Import java.util.List;
Import java.util.concurrent.Callable;
Import java.util.concurrent.ExecutionException;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.Future;
public class Threadaddfuture {public static list<future> futurelist=new arraylist<future> ();
public static void Main (string[] args) throws Interruptedexception, executionexception {int sum=0;
Threadaddfuture add = new Threadaddfuture ();
Executorservice Pool=executors.newfixedthreadpool (4);
for (int i=1;i<=76;) {threadtest thread=add.new threadtest (i,i+24);
Future<integer> future=pool.submit (thread);
Futurelist.add (future);
i+=25; } if (Futurelist!=null && futurelist.size () >0) {for (future<integer> future:futurelist
) {sum+= (Integer) future.get (); } System.out.println ("Total result:" +sum);
Pool.shutdown ();
Class ThreadTest implements callable<integer> {private int begin;
private int end;
public int sum=0;
public threadtest (int begin, int end) {this.begin = begin;
This.end = end;
@Override public Integer Call () throws Exception {for (int i=begin;i<=end;i++) {
Sum+=i;
} System.out.println ("from" +thread.currentthread (). GetName () + "sum=" +sum);
return sum;
}
}
}
Output:
from pool-1-thread-3 sum=1575
from Pool-1-thread-1 sum=325
to pool-1-thread-2 sum=950
from PO Ol-1-thread-4 sum=2200
Total result:5050