title: 多线程编程例子
date: 2019-10-1 19:00:08
updated: 2020-02-27 19:04:06
categories: 多线程
tags:
- 多线程
本博文记录多线程编程的几个案例,包含了常见的几种多线程编程套路。
1.四个线程计算1到100的求和
1.依次调用thread.join(),主线程输出结果。注意:sum为共享变量,访问共享变量时,用synchronized同步
public class ThreadTest {
private static Object LOCK = new Object();
private static int sum = 0;
public static void main(String[] args) throws InterruptedException {
ThreadTest threadTest = new ThreadTest();
Thread t1 = threadTest.new ThreadSum(1,25);
Thread t2 = threadTest.new ThreadSum(26,50);
Thread t3 = threadTest.new ThreadSum(51,75);
Thread t4 = threadTest.new ThreadSum(76,100);
t1.start();
t2.start();
t3.start();
t4.start();
t1.join();
t2.join();
t3.join();
t4.join();
System.out.println("totalSum: " + sum);
}
class ThreadSum extends Thread{
private int begin;
private int end;
public ThreadSum(int begin, int end){
this.begin = begin;
this.end = end;
}
@Override
public void run() {
try {
Thread.sleep(100);//便于测试
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (LOCK){
for(int i=begin; i<=end; ++i){
sum += i;
}
System.out.println(Thread.currentThread().getName() + ": " + sum);
}
}
}
}
out:Thread-1: 950
Thread-0: 1275
Thread-3: 3475
Thread-2: 5050
totalSum: 5050
2.子线程执行完调用countdownlatch.countdown(),主线程调用countdownlatc.await() 等待子线程执行完成,输出结果。 注意:sum为共享变量,访问共享变量时,用synchronized同步
public class ThreadTest {
private static Object LOCK = new Object();
private static int sum = 0;
private static CountDownLatch countDownLatch = new CountDownLatch(4);
public static void main(String[] args) throws InterruptedException {
ThreadTest threadTest = new ThreadTest();
Thread t1 = threadTest.new ThreadSum(1,25);
Thread t2 = threadTest.new ThreadSum(26,50);
Thread t3 = threadTest.new ThreadSum(51,75);
Thread t4 = threadTest.new ThreadSum(76,100);
t1.start();
t2.start();
t3.start();
t4.start();
countDownLatch.await();
System.out.println("totalSum: " + sum);
}
class ThreadSum extends Thread{
private int begin;
private int end;
public ThreadSum(int begin, int end){
this.begin = begin;
this.end = end;
}
@Override
public void run() {
try {
Thread.sleep(100);//便于测试
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (LOCK){
for(int i=begin; i<=end; ++i){
sum += i;
}
System.out.println(Thread.currentThread().getName() + ": " + sum);
}
countDownLatch.countDown();
}
}
}
3.线程池
public class ThreadTest {
private static Object LOCK = new Object();
private static int sum = 0;
private static CountDownLatch countDownLatch = new CountDownLatch(4);
public static void main(String[] args) throws InterruptedException {
ThreadTest threadTest = new ThreadTest();
ExecutorService pool = Executors.newFixedThreadPool(4);
for(int i=1; i<=76; i+=25){
pool.execute(threadTest.new ThreadSum(i, i+24));
}
countDownLatch.await();
pool.shutdown();
System.out.println("totalSum: " + sum);
}
class ThreadSum extends Thread{
private int begin;
private int end;
public ThreadSum(int begin, int end){
this.begin = begin;
this.end = end;
}
@Override
public void run() {
try {
Thread.sleep(100);//便于测试
} catch (InterruptedException e) {
e