代码如下: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();
}
}
{
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();
}
}
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多谢各位了!!!
thread_A.start(); //这个地方,你start之后,马上会调用Thread.sleep( 10000 );使得主线程等待,那么你的程序就暂时挂起了,你让b线程怎么start呢? try
{
Thread.sleep( 500 );
}
catch( InterruptedException e )
{} thread_B.start();
还有线程的运作是随机性很强的!
同样的方法会出现不同的结果!
望再看看书!
这里的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上的阿,为什么书中会这样解释呢????
这里是关键——一旦一个线程包含在一个同步方法中,没有其它线程可以调用相同对象的同步方法。
你这么解释我倒是明白一些了,请问关于java管程的资料或者书籍从什么地方能够获得?
java虚拟机规范之类的书中会有这方面的介绍吗?
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机制,不明白的话可以看一下操作系统原理教程,都会详细讲到的。
对象级别的锁 跟 类级别的锁 的区别是什么啊????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();
}
}
如果C.a()和C.b()都用synchronized修饰。 就是C.a()方法在一个线程运行时,C.b()在别的线程不能运行。
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>>