::Z::Thinking::

::Simple::
文章 - 124,收藏 - , 评论 - 49, trackbacks - 0

1,synchronized一个类的static变量和public void method()并不排斥

2,类的static synchronized方法和类的普通的synchronized方法并不排斥,可以被2个Thread同时访问。

3,static synchronized的方法之间互诉。

4,普通的synchronized方法之间互诉。

/*
 * TestSyncMethod.java
 *
 * Created on 2005年7月3日, 下午9:57
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package threadtest;

/**
 *
 * @author larry
 */
public class TestSyncMethod {
   
    public TestSyncMethod() {
    }
    public static Object staticLock=new Object();
    public static Object staticLock1=new Object();
    private boolean canGo=false;
    public static TestSyncMethod test=new TestSyncMethod();
   
    public static String getThreadName(){
        return Thread.currentThread().getName();
    }
    public synchronized void methodOne(){
        print(getThreadName()+":::enter method one");
        try{
            Thread.sleep(3000);
        }catch(Exception e){}
        print(getThreadName()+":::sleep over");
        while(!canGo){
            try{
                this.wait();
            }catch(Exception e){}
        }
        print(getThreadName()+":::over");
    }   
    public synchronized void methodTwo(){
        print(getThreadName()+":::enter method two");
        canGo=true;
        this.notifyAll();
    }
    public void print(String str){
        System.out.println(str);
    }
    Thread t1=new Thread(new Runnable(){
        public void run(){
            TestSyncMethod.test.methodOne();
        }
    }
    );
   
        Thread t2=new Thread(new Runnable(){
        public void run(){
            TestSyncMethod.test.methodTwo();
        }
    }
    );
   
    Thread t3=new Thread(new Runnable(){
        public void run(){
            print(getThreadName()+":::start t3");
            synchronized(TestSyncMethod.staticLock){
                print(getThreadName()+":::enter lock t3");
                try{
                     Thread.sleep(6000);
                }catch(Exception e){
                }
                print(getThreadName()+":::over");
            }
        }
    }
    );
   
        Thread t4=new Thread(new Runnable(){
        public void run(){
            print(getThreadName()+":::start t4");
            synchronized(TestSyncMethod.staticLock1){
                print(getThreadName()+":::enter lock t4");
                try{
                     Thread.sleep(6000);
                }catch(Exception e){
                }
                print(getThreadName()+":::over");
            }
        }
    }
    );
   
    public static void main(String[] args){
        TestSyncMethod tt=new TestSyncMethod();
        tt.t3.start();
        tt.t4.start();
        tt.t1.start();
        tt.t2.start();
       
    }
}


init:
deps-jar:
Compiling 1 source file to F:\Documents and Settings\larry\ThreadTest\build\classes
compile-single:
run-single:
Thread-6:::start t3
Thread-6:::enter lock t3
Thread-7:::start t4
Thread-7:::enter lock t4
Thread-4:::enter method one
Thread-4:::sleep over
Thread-5:::enter method two
Thread-4:::over
Thread-6:::over
Thread-7:::over
BUILD SUCCESSFUL (total time: 7 seconds)




Trackback: http://tb.donews.net/TrackBack.aspx?PostId=453070


[点击此处收藏本文]  发表于2005年07月03日 10:55 PM




正在读取评论……