如何控制Java中的线程,总结了3种方法...
2015-08-13 15:04
701 查看
问题:利用Java多线程,轮流打印数字,也就是怎么控制线程....
1:通过synchronized的关键字,对类的static final 成员进行Lock,锁住对象,来实现同步。
2.通过AtomicInteger对象,和ExecutorService实现线程之间的同步
3.通过ReentrantLock对象和Condition对象实现线程之间的同步
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(); }
相关文章推荐
- Spring之LoadTimeWeaver——一个需求引发的思考
- Spring使用总结
- Java日志框架——Logback的运行过程
- Spring 学习笔记(二)——装配 bean
- Java 获得纯数字格式的时间
- Java - How Garbage Collection works in Java
- NJUPT JAVA语言 综合图形界面程序设计
- Java多线程编程之Runnable、Thread简介
- 了解 Spring Data JPA
- Java post 请求
- eclipse studio 使用
- struts2 中chain、redirect、redirectaction的区别
- java基础知识实例
- Spring_Transactional_Propagation
- java入门--4110:圣诞老人的礼物-Santa Clau’s Gifts
- java jaxb
- spring事务定义中,@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED) 这样定义有问题吗?
- Java设计模式之抽象工厂模式
- java.lang.OutOfMemoryError: Java heap space
- java设计模式图