学习synchronized中关于notify 和 wait 的菜鸟级问题

来源:互联网 发布:matlab解矩阵方程 编辑:程序博客网 时间:2024/06/08 14:35

关于notify和wait

开始时尝试的代码如下

想通过两个不同的线程进行直接的wait 和 notify

public class Test04 {
private static boolean isDayOrNight = true;
public static void main(String[] args) {
Thread A = new Thread() {
public void run() {
for(int days=0;days<50;days++){
//一个整体
synchronized(Test04.class){
if(!isDayOrNight){ //等待
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("working...");
for(int i=0;i<18;i++){
System.out.print(i+"");
}
System.out.println();

//更变标志
isDayOrNight =false;
notify();
}
}
}
};
Thread B = new Thread() {
public void run() {
for(int days=0;days<50;days++){
//一个整体
synchronized(Test04.class){
if(!isDayOrNight){ //等待
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print("working...");
for(int i=0;i<18;i++){
System.out.print(i+"");
}
System.out.println();

//更变标志
isDayOrNight =false;
notify();
}
}
}
};
A.start();
B.start();
}
}

稍微学过的人看起来应该都会觉得蠢是吧哈哈,不过我也是看了档案后才搞明白到底是怎么回事,

先说一下关于 wait 和notify的介绍吧,还好哥英文不错

下面是notify的

Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object,one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of thewait methods.

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.

This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

  • By executing a synchronized instance method of that object.
  • By executing the body of a synchronized statement that synchronizes on the object.
  • For objects of type Class, by executing a synchronized static method of that class.

Only one thread at a time can own an object's monitor.

这个是wait的


Causes the current thread to wait until another thread invokes the java.lang.Object.notify() method or thejava.lang.Object.notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the callwait(0).

The current thread must own this object's monitor.The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to thenotify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop: 


看看标注的英语红字就好了,大概意思就是 wait 和notify 必须服务于同一个 Object   ,可以说成是  交互,   必须先进行交互后才能互相通知使用到 。有不对的或者可以扩张的地方欢迎各位评论,好做修改谢谢。


最后关于synchronized的用法看了几段文字后觉得说的真是

synchronized是Java中的关键字,是一种同步锁。它修饰的对象有以下几种: 
1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码块的对象; 
2. 修饰一个方法,被修饰的方法称为同步方法,其作用的范围是整个方法作用的对象是调用这个方法的对象; 
3. 修改一个静态的方法,其作用的范围是整个静态方法,作用的对象是这个类的所有对象; 
4. 修改一个类,其作用的范围是synchronized后面括号括起来的部分,作用主的对象是这个类的所有对象

     开始看的时候也就浏览了一下,然后遇到问题解决后,想弄明白是怎么回事的时候回来看看这段解释,真是此中有真意啊。

阅读全文
0 0