代码如下:public class Synchronized_01
{
private synchronized void methord_A()
{
String threadName = Thread.currentThread().getName(); System.out.println( "entering methord_A: " + threadName );
try
{
Thread.sleep( 10000 );
}
catch( InterruptedException e )
{}

System.out.println( "leaving methord_A: " + threadName );
}

private  synchronized void methord_B()
{
String threadName = Thread.currentThread().getName();

System.out.println( "entering methord_B: " + threadName );
try
{
Thread.sleep( 1000 );
}
catch( InterruptedException e )
{}
System.out.println( "leaving methord_B: " + threadName );
} public static void main( String[] args )
{
final Synchronized_01 syn = new Synchronized_01() ;

Runnable runA = new Runnable()
{
public void run()
{
syn.methord_A();
}
};// end of new runA Runnable runB = new Runnable()
{
public void run()
{
syn.methord_B();
}
};// end of new runB Thread thread_A = new Thread( runA, "A" );
Thread thread_B = new Thread( runB, "B" ); thread_A.start(); try
{
Thread.sleep( 500 );
}
catch( InterruptedException e )
{} thread_B.start();
}
}

解决方案 »

  1.   

    输出的结果是:
    entering methord_A:     A
    leaving methord_A:      A
    entering methord_B:     B
    leaving methord_B:      B如果把synchronized修饰去掉
    输出结果是:
    entering methord_A:     A
    entering methord_B:     B
    leaving methord_B:      B
    leaving methord_A:      A多谢各位了!!!
      

  2.   

    虽然你使用了多线程,但是你的程序总运行的角度看,还是单线程的。
    thread_A.start(); //这个地方,你start之后,马上会调用Thread.sleep( 10000 );使得主线程等待,那么你的程序就暂时挂起了,你让b线程怎么start呢? try
    {
    Thread.sleep( 500 );
    }
    catch( InterruptedException e )
    {} thread_B.start();
      

  3.   

    你还是没有搞清楚 synchronized的概念!
    还有线程的运作是随机性很强的!
    同样的方法会出现不同的结果!
    望再看看书!
      

  4.   

    to zhang21cnboy(事了抚衣去,不留身与名):'thread_A.start(); //这个地方,你start之后,马上会调用Thread.sleep( 10000 );'
    这里的Thread.sleep(10000),我理解是这样的:不是说这个代码写在哪个类里面,就是哪个类的实例的线程就进行sleep,而应当是说当前执行到这个语句的线程进行sleep,所以这里是指thread_A进行sleep。
    主线程的sleep是在执行
                      try
    {
    Thread.sleep( 500 );
    }
    的时候进行的。但是由于主线程sleep的时间是500ms,要远远小于10000ms,所以,在Thread_a回复执行之前,主线程已经回复执行,并且运行thread_B.start()。我是这么理解Thread.sleep(XXX)的。不知道对不对,还请执教!!!============================================================================to tiger_wkh52741(走走.跑跑.瞧瞧!) :
    对于这个程序,我的运行结果就是这样的。我承认一般的多线程程序在执行顺序上有一定的随机性。但是这里synchronized的修饰,导致程序结果是一定的。类似的代码可以在'java线程编程’这本书中找到,P124---P125,在P125下面解释说,thread_b是由于要获取对于对象级的锁的排斥访问权限,而被阻塞。我不太理解“对象级的锁的排斥访问权限”这个意思,我的synchronized是加在methord上的阿,为什么书中会这样解释呢????
      

  5.   

    java通过管程(monitor)来防止共享的资源被多个线程操纵。一旦线程进入管程,所有线程必须等待直到该线程退出了管程。每个对象都拥有自己的隐式管程,当对象的同步方法被调用时管程自动载入。一旦一个线程包含在一个同步方法中,没有其它线程可以调用相同对象的同步方法。这也就是为什么加上synchronized后只有第一个线程执行完才会执行第二个线程的原因了。
    这里是关键——一旦一个线程包含在一个同步方法中,没有其它线程可以调用相同对象的同步方法。
      

  6.   

    to nm_2j(夜奔) :
    你这么解释我倒是明白一些了,请问关于java管程的资料或者书籍从什么地方能够获得?
    java虚拟机规范之类的书中会有这方面的介绍吗?
      

  7.   

    synchronized方法就是这个作用啊!
      

  8.   

    Java2 参考大全(第4版)线程那一章有讲,需要的话留下email
      

  9.   

    偶来解释一下,其实很简单的道理:首先,synchronized只能修饰实例方法,不能修饰static方法,原因是static方法没有this句柄参数;而“对象级的锁的排斥访问权限”需要用到this句柄;方法调用 A.methodA()实际上在二进制一级肯定是methodA(A)的调用形式,此时我们说methodA运行在对象A上;如果一个线程中有A.methodA()这个语句,那么当执行此语句时,该线程就运行在A上;对于synchronized方法,你可以这样理解:
    A.methodA()可以看成如下过程:
    method (A)
    {
       ENTER A;//Require lock on A
       ...
       LEAVE A;//Release lock on A
    }
    同样,A.methodB()可以看成:
    methodB(A)
    {
       ENTER A;//Require lock on A   ...
       LEAVE A;//Release lock on A}JVM会保证在任意时刻,只有一个线程可以获得一个对象的锁,当然,只有synchronized方法或synchronized程序段才受到此限制;java的synchronized同步机制的实现方式很多,最常用的是Monitor机制,不明白的话可以看一下操作系统原理教程,都会详细讲到的。
      

  10.   

    看了一周的thread,越看越糊涂!!!!
    对象级别的锁 跟 类级别的锁 的区别是什么啊????to goldenhua(深深地爱上了你) :
    但是我在书中看到,synchronized可以用在static方法上啊public class StaticBlock extends Object {
             //注意这里
    public static synchronized void staticA() {
    System.out.println("entering staticA()"); try { Thread.sleep(5000); } 
    catch ( InterruptedException x ) { } System.out.println("leaving staticA()");
    } public static void staticB() {
    System.out.println("entering staticB()"); synchronized ( StaticBlock.class ) {
    System.out.println(
    "in staticB() - inside sync block"); try { Thread.sleep(2000); } 
    catch ( InterruptedException x ) { }
    } System.out.println("leaving staticB()");
    } public static void main(String[] args) {
    Runnable runA = new Runnable() {
    public void run() {
    StaticBlock.staticA();
    }
    }; Thread threadA = new Thread(runA, "threadA");
    threadA.start(); try { Thread.sleep(200); } 
    catch ( InterruptedException x ) { } Runnable runB = new Runnable() {
    public void run() {
    StaticBlock.staticB();
    }
    }; Thread threadB = new Thread(runB, "threadB");
    threadB.start();
    }
    }
      

  11.   

    哦,是可以的,非常抱歉,我信口胡说了。static方法是在类的class对象上同步的。
      

  12.   

    简单的说
      如果C.a()和C.b()都用synchronized修饰。  就是C.a()方法在一个线程运行时,C.b()在别的线程不能运行。
      

  13.   

    When one thread calls a synchronized method, it is guaranteed that the method will finish before another thread can execute any synchronized method on the same object. When one thread calls transfer and then another thread also calls transfer, the second thread cannot continue. Instead, it is deactivated and must wait for the first thread to finish executing the transfer method.
    Object Lock:
    Technically, each object has a lock count that counts how many synchronized methods the lock owner has called. Each time a new synchronized method is called, the lock count is increased. Each time a synchronized method terminates (either because of a normal return or because of an uncaught exception), the lock count is decremented. When the lock count reaches zero, the thread gives up the lock.
    from<<core java2 volumn II>>