synchronized及wait()/notify()的一些实验

最近在项目中运用多线程比较多,对synchronized,wait(),notify(),做了一些实验。

 

1、针对方法的synchronized实际上是对该对象加锁。

package com.test.wait_notify;

/**
 * 锅巴
 * 描述
 * @version 1.0 2010-8-10
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        Main main = new Main();
        (new Thread(new MyThread(main))).start();
        Thread.sleep(1000);
        main.test2();
    }
    
    public synchronized void test3() throws InterruptedException{
        System.out.println(Thread.currentThread().getName() + " test3()");
        notify();
        wait();
    }
    
    public synchronized void test2(){
        for(int i=0; i<10; i++){
            System.out.println(Thread.currentThread().getName() + " test2()");
        }
    }
    
    public synchronized void test1() throws InterruptedException{
        System.out.println(Thread.currentThread().getName() + " test1() start");
        //wait();
        Thread.sleep(5000);
        System.out.println(Thread.currentThread().getName() + " test1() end");
    }
}

 

class MyThread implements Runnable{

    private Main main;
    
    public MyThread(Main main){
        this.main = main;
    }
    
    public void run() {
        // TODO Auto-generated method stub
        try {
            main.test1();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}

 

运行结果

Thread-0 test1() start
Thread-0 test1() end
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()

MyThread.run中调用synchronized void test1() ,这时对main对象加锁

main.test2()要等待main锁释放。

 

结论:当某线程调用某synchronized 修饰的方法时,会对该对象加锁。其他对象在调用其他synchronized 修饰方法时要等该获得该对象锁。

 

2、wait(),notify(),notifyAll() 必须在 synchronized 修饰内才有用。

 

3、wait()是让线程等待,放弃执行权力,并释放锁。

/**
 * 锅巴
 * 描述
 *
 * @version 1.0 2010-8-10
 */
public class Main {

    public static void main(String[] args) throws InterruptedException {
        Main main = new Main();
        (new Thread(new MyThread(main))).start();
        (new Thread(new MyThread(main))).start();
        (new Thread(new MyThread(main))).start();
        Thread.sleep(1000);
        main.test2();
        Thread.sleep(1000);
        main.test3();
        main.test3();
        main.test3();
    }
    
    public synchronized void test3() throws InterruptedException{
        System.out.println(Thread.currentThread().getName() + " test3()");
        notify();
    }
    
    public synchronized void test2(){
        for(int i=0; i<10; i++){
            System.out.println(Thread.currentThread().getName() + " test2()");
        }
    }
    
    public synchronized void test1() throws InterruptedException{
        System.out.println(Thread.currentThread().getName() + " test1() start");
        wait();
        System.out.println(Thread.currentThread().getName() + " test1() end");
    }
}

class MyThread implements Runnable{

    private Main main;
    
    public MyThread(Main main){
        this.main = main;
    }
    
    public void run() {
        // TODO Auto-generated method stub
        try {
            main.test1();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}

 执行结果

 

Thread-0 test1() start
Thread-2 test1() start
Thread-1 test1() start
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test2()
main test3()
main test3()
main test3()
Thread-0 test1() end
Thread-1 test1() end
Thread-2 test1() end

 

在各线程调用main.test1()时碰到wait(),休眠并释放锁,其他线程可以进入。

main.test2()在各线程都wait()时调用

 

你可能感兴趣的:(thread,多线程)