给大家带来一个项目中实用的多线程用法,可以直接在项目中爽用,建议在中心配置完线程池使用
public static void main(String[] args) {
ReentrantLock reentrantLock = new ReentrantLock();
reentrantLock.lock();
List<Integer> list = new ArrayList<>();
new Thread(() -> {
for (int a=0;a<10;a++){
list.add(a);
}
System.out.println(" = " +Thread.currentThread().getName());
}, "02").start();
new Thread(() ->{
for (int a=10;a<20;a++){
list.add(a);
}
System.out.println(" = " +Thread.currentThread().getName());
},"01").start();
reentrantLock.unlock();
System.out.println("list = " + list);
}