package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
*/
public class Service {
public void testMethod(Object lock){
try{
synchronized(lock){
System.out.println("wait begin");
Thread.sleep(400);
System.out.println("end wait");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
package ThreadTest;
import javaThread.Threadd;
/**
* Created by Administrator on 2016/11/2.
*/
public class ThreadA extends Thread{
private Object lock;
public ThreadA(Object lock){
this.lock=lock;
}
public void run(){
Service service=new Service();
service.testMethod(lock);
}
}
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
*/
public class ThreadB extends Thread{
private Object lock;
public ThreadB(Object lock){
this.lock=lock;
}
public void run(){
Service service=new Service();
service.testMethod(lock);
}
}
sleep 不释放锁
结果
如果把service的wait答案是
验证wait 释放锁和notify不释放锁
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
*/
public class Service1 {
public void testMethod(Object lock){
try{
synchronized(lock){
System.out.println("begin wait threadname="+Thread.currentThread().getName());
lock.wait();
System.out.println("end wait threadname="+Thread.currentThread().getName());
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
public void synNotifyMethod(Object lock){
try{
synchronized(lock){
System.out.println("notify start threadname="+Thread.currentThread().getName());
lock.notify();
Thread.sleep(5000);
System.out.println("notify end threadname="+Thread.currentThread().getName());
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
*/
public class ThreadC extends Thread {
private Object lock;
public ThreadC(Object lock){
super();
this.lock=lock;
}
public void run(){
Service1 service1=new Service1();
service1.testMethod(lock);
}
}
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
*/
public class NotifyThread extends Thread {
private Object lock;
public NotifyThread(Object lock){
super();
this.lock=lock;
}
public void run(){
Service1 service=new Service1();
service.synNotifyMethod(lock);
}
}
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
* 代码结果说明必须执行notify完方法才释放锁
* 执行完同步代码块就会释放锁
* 在执行同步代码块的过程中,遇到异常锁也会被释放
* 在执行代码块的过程中,执行了锁所属对象的wait方法,这个想成会直接释放锁,而此线程会进入线程等待池中等待被唤醒
* notify之唤醒一个线程,自己下去测试 很简单 方法随机唤醒一个线程 唤醒所有线程用notifyall
* wait(long)等待某一时间,如果超过这个时间自动唤醒
*/
public class TestNotifyAndWait {
public static void main(String [ ] args){
Object lock=new Object();
ThreadC th1 = new ThreadC(lock);
NotifyThread th2 = new NotifyThread(lock);
th1.start();
th2.start();
}
}
结果
还有就是notify出现在wait前,解决方案
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
* 不可以通知太早 改正方法
*/
public class MyRun {
private String lock="";
private Runnable runnablea=new Runnable() {
@Override
public void run() {
try{
synchronized(lock){
System.out.println("wait start");
lock.wait();
System.out.println("end wait");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
private Runnable runnableb=new Runnable() {
@Override
public void run() {
synchronized(lock){
System.out.println("start notify");
lock.notify();
System.out.println("end notify");
}
}
};
public static void main(String [] args){
MyRun run=new MyRun();
Thread a = new Thread(run.runnablea);
a.start();
Thread b = new Thread(run.runnableb);
b.start();
MyRun runS=new MyRun();
Thread c= new Thread(runS.runnablea);
Thread d = new Thread(runS.runnableb);
c.start();
d.start();
}
}
修改
package ThreadTest;
/**
* Created by Administrator on 2016/11/2.
*/
public class MyRun1 {
private String lock=new String("");
private boolean isFirstRnuB=false;
private Runnable runnablea=new Runnable() {
@Override
public void run() {
try{
synchronized(lock){
while (isFirstRnuB==false){
System.out.println("wait start");
lock.wait();
System.out.println("end wait");
}
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
};
private Runnable runnableb=new Runnable() {
@Override
public void run() {
synchronized(lock){
System.out.println("start notify");
lock.notify();
System.out.println("end notify");
isFirstRnuB=true;
}
}
};
public static void main(String [] args){
MyRun1 runS=new MyRun1();
Thread c= new Thread(runS.runnablea);
Thread d = new Thread(runS.runnableb);
d.start();
c.start();
}
}