就是三个一起玩游戏,必须等到三个人都通关了才能进入到下一关。
案例:使用多线程完成累计求和的过程:
有几行就有几个线程累计求和。
线程的基本套路就是
一个实体的Demo类,里面是sync的方法里面是要实现的逻辑。
一个类继承runnable,构造函数传入实体类的实例,不断的去调用实体类的sync方法。
main函数 ,new实体类,new Thread(new runnanle(实体类)).start();
package thread.tb4;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Demo {
private int[] nums;
public Demo(int line) {
nums = new int[line];
}
public void calc(String line, int index) {//传入是参数 第几行
String[] nus = line.split(",");//切分出每个值
int total = 0;
for (String num : nus) {
total += Integer.parseInt(num);
}
nums[index] = total; // 把计算的结果放到数组中指定的位置
System.out.println(Thread.currentThread().getName() + " do work... " + line + " result:" + total);
}
public void sum() {
System.out.println("add total begin... ");
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
}
System.out.println("final:" + total);
}
public static void main(String[] args) {
List<String> contents = readFile(); // 读文件到list
int lineCount = contents.size(); // 几行
Demo d = new Demo(lineCount);
for (int i = 0; i < lineCount; i++) { // 每个线程自己计算结果
final int j = i;
new Thread(new Runnable() {
@Override
public void run() {
d.calc(contents.get(j), j);
}
}).start();
}
while (Thread.activeCount() > 2) {//执行过程复杂会浪费cpu
}
d.sum();
}
private static List<String> readFile() {
List<String> contents = new ArrayList<>();
String line = null;
BufferedReader br = null;
try {
//br = new BufferedReader(new FileReader("e:\\nums.txt"));
// D:\CODE_My\threadyzy\src\main\java\thread\tb4\nums.txt
br = new BufferedReader(new FileReader("D:\\CODE_My\\threadyzy\\src\\main\\java\\thread\\tb4\\nums.txt"));
while ((line = br.readLine()) != null) {
contents.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return contents;
}
}
注意这句浪费cpu的。
while (Thread.activeCount() > 2) {//执行过程复杂会浪费cpu
}
用CountDownLatch改写
package com.roocon.thread.tb4;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class Demo2 {
private int[] nums;
public Demo2(int line) {
nums = new int[line];
}
public void calc(String line, int index, CountDownLatch latch) {
String[] nus = line.split(","); // 切分出每个值
int total = 0;
for (String num : nus) {
total += Integer.parseInt(num);
}
nums[index] = total; // 把计算的结果放到数组中指定的位置
System.out.println(Thread.currentThread().getName() + " 执行计算任务... " + line + " 结果为:" + total);
latch.countDown();//2.执行完之后就countDown
}
public void sum() {
System.out.println("汇总线程开始执行... ");
int total = 0;
for (int i = 0; i < nums.length; i++) {
total += nums[i];
}
System.out.println("最终的结果为:" + total);
}
public static void main(String[] args) {
List<String> contents = readFile();
int lineCount = contents.size();
CountDownLatch latch = new CountDownLatch(lineCount);//0.在main函数设置的,就是有几行
Demo2 d = new Demo2(lineCount);
for (int i = 0; i < lineCount; i++) {
final int j = i;
new Thread(new Runnable() {
@Override
public void run() {
d.calc(contents.get(j), j, latch);//1.run方法传入CountDownLatch
}
}).start();
}
try {
latch.await();//3.await
} catch (InterruptedException e) {
e.printStackTrace();
}
d.sum();
}
private static List<String> readFile() {//读文件分方法
List<String> contents = new ArrayList<>();
String line = null;
BufferedReader br = null;
try {
//br = new BufferedReader(new FileReader("e:\\nums.txt"));
br = new BufferedReader(new FileReader("D:\\CODE_My\\thread\\src\\com\\roocon\\thread\\tb4\\nums.txt"));
while ((line = br.readLine()) != null) {
contents.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return contents;
}
}
---------------------------------tb4----------------------------------------------
小结CountDownLatch的用法
1.主线程new一个CountDownLatch latch
2.在run每个线程调用都传入latch
3.方法体执行完之后就latch.countDown()
4.主线程await