标签:blog io 使用 ar java for sp div art
半路出家学习java, 花了几分钟简单看了。在早上那个例子上稍微改了下,
我的理解是在java synchronized只是个线程同步标志,但是不会堵塞线程,而wait会堵塞调用者线程,类windows的堵塞函数,而notify相当于解锁(个人把这个过程当临界区来理解的),下边例子主要是,默认先运行的线程test1会先执行, 我稍微改了下, 让第一个线程挂起,等第二个线程执行完,再执行第一个线程
代码
import java.util.concurrent.CountDownLatch; public class CountDown { private static CountDownLatch connectedSignal = new CountDownLatch(50); static class MyThread implements Runnable { public void run() { Thread current = Thread.currentThread(); System.out.println(current.getName()); synchronized (connectedSignal) { Thread thread = Thread.currentThread(); String name = thread.getName(); if (name.equals("test1")){ try { connectedSignal.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (int i = 0; i < 25; ++i) { sale(); } if (name.equals("test2")){ connectedSignal.notify(); } } } public synchronized void sale() { if (connectedSignal.getCount() > 0) { Thread current = Thread.currentThread(); connectedSignal.countDown(); System.out.println("Thread id" + current.getId() + "value = " + connectedSignal.getCount()); } } } public static void main(String[] args) throws InterruptedException { MyThread my = new MyThread(); Thread t1 = new Thread(my, "test1"); Thread t2 = new Thread(my, "test2"); t1.start(); t2.start(); connectedSignal.await(); // 等待结束 System.out.println(connectedSignal.getCount()); } }
又花了几分钟稍微改了下, 让两个线程轮流执行
import java.util.concurrent.CountDownLatch; public class CountDown { private static CountDownLatch connectedSignal = new CountDownLatch(50); static class MyThread implements Runnable { public void run() { Thread current = Thread.currentThread(); System.out.println(current.getName()); synchronized (connectedSignal) { Thread thread = Thread.currentThread(); String name = thread.getName(); // if (name.equals("test1")){ // try { // connectedSignal.wait(); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } for (int i = 0; i < 25; ++i) { if (name.equals("test1") && (i % 2 == 0)){ try { sale(); connectedSignal.notify(); connectedSignal.wait(); connectedSignal.notify(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ sale(); connectedSignal.notify(); try { connectedSignal.wait(); connectedSignal.notify(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } // if (name.equals("test2")){ // connectedSignal.notify(); // } } } public synchronized void sale() { if (connectedSignal.getCount() > 0) { Thread current = Thread.currentThread(); connectedSignal.countDown(); System.out.println("Thread id" + current.getId() + "value = " + connectedSignal.getCount()); } } } public static void main(String[] args) throws InterruptedException { MyThread my = new MyThread(); Thread t1 = new Thread(my, "test1"); Thread t2 = new Thread(my, "test2"); t1.start(); t2.start(); connectedSignal.await(); // 等待结束 System.out.println(connectedSignal.getCount()); } }
结果和我想的一样。感觉本身线程使用挺简单的,java估计是为了开发者更方便开发, 自己封装再提供了更多的功能,反倒让我很不习惯。
纯粹本人观点, 有错误欢迎指出.
标签:blog io 使用 ar java for sp div art
原文地址:http://www.cnblogs.com/chengxin1982/p/3997215.html