java notify 用法_Java notify的使用

半路出家学习java, 花了几分钟简单看了。在早上那个例子上稍微改了下,

notify 对象上必须使用 synchronized

我的理解是在java synchronized只是个线程同步标志,但是不会堵塞线程,而wait会堵塞调用者线程,类windows的堵塞函数,而notify相当于解锁(个人把它当成windows开发的event来理解的, wait 相当于 resertevent加 WaitForSingleObject 挂起线程,  对象相当于event, notify相当于 setevent),下边例子主要是,默认先运行的线程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估计是为了开发者更方便开发, 自己封装再提供了更多的功能,反倒让我很不习惯。

wait线程之间的等待,waitFor线程等待进程.

纯粹本人观点, 有错误欢迎指出.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值