如何控制Java中的线程,总结了3种方法...
2024-10-17 23:14:18
问题:利用Java多线程,轮流打印数字,也就是怎么控制线程....
1:通过synchronized的关键字,对类的static final 成员进行Lock,锁住对象,来实现同步。
private int id;
private static int n = 0;
private static final ThreadTest lock = new ThreadTest(); public ThreadTest(int id) {
this.id = id;
}
public ThreadTest () { }
@Override
public void run() {
while (n < 100) {
synchronized (lock) {
while ((n % 5 == 0) && (n / 5) % 5 != id) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (n < 100) {
System.out.print("Thread-" + (id+1) + " : " + " " + (n + 1)
+ " " + (n + 2) + " " + (n + 3) + " " + (n + 4)
+ " " + (n + 5) + "\n");
n += 5;
}
lock.notifyAll();
}
}
}
2.通过AtomicInteger对象,和ExecutorService实现线程之间的同步
private AtomicInteger atomicInteger=new AtomicInteger(0);
private static final int max=20; class Thread1 implements Runnable{
private int mark=0;
public Thread1(int i){
this.mark=i;
}
public void run() {
while(atomicInteger.get()<max){
if(atomicInteger.get()%5==mark){
System.out.println("线程Thread"+(mark+1)+"打印:"+(atomicInteger.get()*5+1)+" "
+(atomicInteger.get()*5+2)+" "+(atomicInteger.get()*5+3)+" "
+(atomicInteger.get()*5+4)+" "+(atomicInteger.get()*5+5));
atomicInteger.getAndIncrement();
}
}
}
}
3.通过ReentrantLock对象和Condition对象实现线程之间的同步
private int state = 1;
private int n = 1;
private ReentrantLock lock=new ReentrantLock();
private Condition condition1=lock.newCondition();
private Condition condition2=lock.newCondition();
private Condition condition3=lock.newCondition();
@Override
public void run(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try {
lock.lock();
while(state!=1)
try{
condition1.await();
}
catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+": ");
for (int j = 0; j < 5; j++) {
System.out.print(n+++" ");
}
System.out.println();
state=2;
condition2.signal();
} finally{
lock.unlock();
}
}
}
},"线程1").start(); new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try{
lock.lock();
while(state!=2)
try {
condition2.await();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+": ");
for (int j = 0; j < 5; j++) {
System.out.print(n+++" ");
}
System.out.println();
state=3;
condition3.signal();
}
finally{
lock.unlock();
}
}
}
},"线程2").start(); new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try{
lock.lock();
while(state!=3)
try {
condition3.await();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
System.out.print(Thread.currentThread().getName()+": ");
for (int j = 0; j < 5; j++) {
System.out.print(n+++" ");
}
System.out.println();
state=1;
condition1.signal();
}finally{
lock.unlock();
}
}
}
},"线程3").start();
}
最新文章
- javascript面试题:如何把一句英文每个单词首字母大写?
- HTML—marquee
- 场景9 深入RAC运行原理
- VS2008基于对话框的MFC上位机串口通信(C++实现)简单例程
- 鼠标悬浮图片时弹出透明提示图层的jQuery特效
- ADO.NET通用数据库访问类
- MySQL源码:索引相关的数据结构
- oracle的一知半解
- python笔记之subprocess模块
- tera term 残ALT债券
- java设计模式之一工厂模式
- Mac, Linux中配置Latex中文字体
- Mysql数据库主从配置
- hdu 5052 树链剖分
- Dynamics CRM2011 通过DeveloperToolkit在VS中部署遇到的问题
- net core体系-web应用程序-4asp.net core2.0 项目实战(1)-3项目架构说明
- Java 输入/输出——处理流(ObjectIO)
- WinForm下窗体权限设计
- Redux DevTools浏览器插件调试redux
- Python 工匠:编写条件分支代码的技巧
热门文章
- Git打包文件
- DECO 一个REACT NAtive 开发IDE工具
- android studio从1.5更新到2.0后terminal无法运行gradle命令,提示无法找到gradle命令
- hdu 1002.A + B Problem II 解题报告
- 【leetcode】Binary Tree Level Order Traversal I &; II
- 【leetcode】Kth Largest Element in an Array (middle)☆
- August 27th 2016 Week 35th Saturday
- 面向对象的小demo
- 关于KVC、KVO
- MyString(重写String)