序言
最近又最新学习了Java基础,有有了新的体会。在学习Java多线程的时候,其中很重要的部分就是线程同步,曾经有一个问题是开三个线程,每个线程打印一个字母,依次是ABC。现在我把这一类的需求都封装到一个有序线程类中,大家有兴趣的可以看看。
源码
OrderedThread
package com.zgh.io.file;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 有序的线程
* 在构造的时候传入线程组的名字,和需要重复执行的Runable对象。
* 线程将会以创建的顺序,有序执行。
* 如果要关闭某个线程组,可以调用OrderedThread.stopGroup();
* @author zhuguohui
*
*/
public class OrderedThread extends Thread {
private Runnable repeat;
private String groupName;
//一个保存所有线程组的map,通过不同名字区分。
private static Map<String, List<OrderedThread>> sMaps = new HashMap<>();
//保存每个线程组下一个要执行的OrderedThread对象的index值
private static Map<String, Integer> sNextStepMap = new HashMap<>();
private boolean runing = true;
private int mStep = 0;
/**
*
* @param groupName
* 组名
* @param repeat
* 需要重复执行的代码
*/
public OrderedThread(String groupName, Runnable repeat) {
this.repeat = repeat;
this.groupName = groupName;
// 注册线程
register(groupName, this);
}
private static synchronized void register(String groupName, OrderedThread thread) {
List<OrderedThread> list = sMaps.get(groupName);
if (list == null) {
list = new ArrayList<OrderedThread>();
sMaps.put(groupName, list);
sNextStepMap.put(groupName, 0);
}
// 添加到list中
list.add(thread);
thread.setSetp(list.indexOf(thread));
}
private void setSetp(int step) {
this.mStep = step;
}
@Override
public void run() {
while (runing) {
canDo();
repeat.run();
nextStep();
}
}
private void exit() {
this.runing = false;
}
private void canDo() {
// 获取锁对象
List list = sMaps.get(groupName);
synchronized (list) {
// 判断是否执行到当前步骤了
int nextStep = sNextStepMap.get(groupName);
//此处一定要使用循环,当线程被唤醒的时候不一定可以执行
while (nextStep != mStep && runing) {
try {
// 如果不是当前步骤则等待
list.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nextStep = sNextStepMap.get(groupName);
}
}
}
/**
* 进行下一步
*/
private void nextStep() {
// 获取锁对象
List list = sMaps.get(groupName);
synchronized (list) {
// 获取下一步
synchronized (sNextStepMap) {
int nextStep = sNextStepMap.get(groupName);
nextStep++;
//如果已经是最后一步,则循环到第一步
if (nextStep == list.size()) {
nextStep = 0;
}
sNextStepMap.put(groupName, nextStep);
}
// 唤醒线程
list.notifyAll();
}
}
//关闭线程组
public static synchronized void stopGroup(String groupName) {
List<OrderedThread> list = sMaps.get(groupName);
if (list == null) {
System.err.println("没有线程组" + groupName + " 或者线程组已关闭");
return;
}
synchronized (list) {
for (OrderedThread thread : list) {
thread.exit();
}
list.notifyAll();
}
sMaps.remove(groupName);
System.out.println("线程组(" + groupName + ") 关闭成功");
}
}
测试
ABC测试
package com.zgh.io.file;
public class Demo10 {
public static void main(String[] args) throws InterruptedException {
String abcGroup = "ABC";
String moveGroup = "move";
OrderedThread threadA = new OrderedThread(abcGroup, new Runnable() {
@Override
public void run() {
System.out.println("A");
}
});
OrderedThread threadB = new OrderedThread(abcGroup, new Runnable() {
@Override
public void run() {
System.out.println("B");
}
});
OrderedThread threadC = new OrderedThread(abcGroup, new Runnable() {
@Override
public void run() {
System.out.println("C");
}
});
threadA.start();
threadB.start();
threadC.start();
// 三秒钟后关闭abcGroup
Thread.sleep(3000);
OrderedThread.stopGroup(abcGroup);
}
}
结果
这里写图片描述
红绿灯测试
package com.zgh.io.file;
public class Demo11 {
public static void main(String[] args) throws InterruptedException {
String moveGroup = "move";
OrderedThread threadCar = new OrderedThread(moveGroup, new Runnable() {
@Override
public void run() {
System.out.println("汽车通过");
}
});
OrderedThread threadPerson = new OrderedThread(moveGroup, new Runnable() {
@Override
public void run() {
System.out.println("行人通过");
}
});
threadCar.start();
threadPerson.start();
}
}
结果
这里写图片描述同时执行两个线程组
package com.zgh.io.file;
public class Demo09 {
public static void main(String[] args) throws InterruptedException {
String abcGroup = "ABC";
String moveGroup = "move";
OrderedThread threadA = new OrderedThread(abcGroup, new Runnable() {
@Override
public void run() {
System.out.println("A");
}
});
OrderedThread threadB = new OrderedThread(abcGroup, new Runnable() {
@Override
public void run() {
System.out.println("B");
}
});
OrderedThread threadC = new OrderedThread(abcGroup, new Runnable() {
@Override
public void run() {
System.out.println("C");
}
});
OrderedThread threadCar = new OrderedThread(moveGroup, new Runnable() {
@Override
public void run() {
System.out.println("汽车通过");
}
});
OrderedThread threadPerson = new OrderedThread(moveGroup, new Runnable() {
@Override
public void run() {
System.out.println("行人通过");
}
});
threadA.start();
threadB.start();
threadC.start();
threadCar.start();
threadPerson.start();
// 三秒钟后关闭abcGroup
Thread.sleep(3000);
OrderedThread.stopGroup(abcGroup);
}
}
效果
这里写图片描述总结
学习java也有近3年了,以前学习这一块脑袋一直是荤的,我一直深知自己的基础薄弱,所以一直在自学计算机原理,编译原理,算法等。现在又重头来学这一块,发现思路清晰了很多。知道线程和进程的区别,知道在内存中发生了什么,知道一条语句会被编译成多条机器代码。果然有些事情还是需要水到渠成才行,问渠那得清如许,为有源头活水来。现在很多人抱怨工作不好找,但是我觉得很多人连编程都还没有入门,前段时间帮朋友改了一下他们公司的Android开发写的代码,看的我想吐血,很多东西我都直接重写,我想我以前也许就是那个样子的吧,简直是水货,而且全然不自知。愿我能降伏其心,在编程的路上走的更稳健,更长远一些,与君共勉。
网友评论