当前位置: 技术问答>java相关
国庆散分&概念
来源: 互联网 发布时间:2015-09-21
本文导语: 概念问题synchronized的用法(方法和表达式),该在什么时候用,举个例子。 我没钱买书,光看MSDN,不大了解。 | A synchronized statement acquires a mutual-exclusion lock (§17.13) on behalf of the execut...
概念问题synchronized的用法(方法和表达式),该在什么时候用,举个例子。
我没钱买书,光看MSDN,不大了解。
我没钱买书,光看MSDN,不大了解。
|
A synchronized statement acquires a mutual-exclusion lock (§17.13) on behalf of the executing thread, executes a block, then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock. SynchronizedStatement:
synchronized ( Expression ) Block
The type of Expression must be a reference type, or a compile-time error occurs. A synchronized statement is executed by first evaluating the Expression. If evaluation of the Expression completes abruptly for some reason, then the synchronized statement completes abruptly for the same reason. Otherwise, if the value of the Expression is null, a NullPointerException is thrown. Otherwise, let the non-null value of the Expression be V. The executing thread locks the lock associated with V. Then the Block is executed. If execution of the Block completes normally, then the lock is unlocked and the synchronized statement completes normally. If execution of the Block completes abruptly for any reason, then the lock is unlocked and the synchronized statement then completes abruptly for the same reason. Acquiring the lock associated with an object does not of itself prevent other threads from accessing fields of the object or invoking unsynchronized methods on the object. Other threads can also use synchronized methods or the synchronized statement in a conventional manner to achieve mutual exclusion. The locks acquired by synchronized statements are the same as the locks that are acquired implicitly by synchronized methods; see §8.4.3.5. A single thread may hold a lock more than once. The example:
class Test {
public static void main(String[] args) {
Test t = new Test();
synchronized(t) {
synchronized(t) {
System.out.println("made it!");
}
}
}
}
prints: made it!
This example would deadlock if a single thread were not permitted to lock a lock more than once.
A synchronized method acquires a lock before it executes. For a class (static) method, the lock associated with the Class object for the method's class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used. These are the same locks that can be used by the synchronized statement; thus, the code:
class Test {
int count;
synchronized void bump() { count++; }
static int classCount;
static synchronized void classBump() {
classCount++;
}
}
has exactly the same effect as:
class BumpTest {
int count;
void bump() {
synchronized (this) {
count++;
}
}
static int classCount;
static void classBump() {
try {
synchronized (Class.forName("BumpTest")) {
classCount++;
}
} catch (ClassNotFoundException e) {
...
}
}
}
The more elaborate example:
public class Box {
private Object boxContents;
public synchronized Object get() {
Object contents = boxContents;
boxContents = null;
return contents;
}
public synchronized boolean put(Object contents) {
if (boxContents != null)
return false;
boxContents = contents;
return true;
}
}
defines a class which is designed for concurrent use. Each instance of the class Box has an instance variable contents that can hold a reference to any object. You can put an object in a Box by invoking put, which returns false if the box is already full. You can get something out of a Box by invoking get, which returns a null reference if the box is empty. If put and get were not synchronized, and two threads were executing methods for the same instance of Box at the same time, then the code could misbehave. It might, for example, lose track of an object because two invocations to put occurred at the same time.
synchronized ( Expression ) Block
The type of Expression must be a reference type, or a compile-time error occurs. A synchronized statement is executed by first evaluating the Expression. If evaluation of the Expression completes abruptly for some reason, then the synchronized statement completes abruptly for the same reason. Otherwise, if the value of the Expression is null, a NullPointerException is thrown. Otherwise, let the non-null value of the Expression be V. The executing thread locks the lock associated with V. Then the Block is executed. If execution of the Block completes normally, then the lock is unlocked and the synchronized statement completes normally. If execution of the Block completes abruptly for any reason, then the lock is unlocked and the synchronized statement then completes abruptly for the same reason. Acquiring the lock associated with an object does not of itself prevent other threads from accessing fields of the object or invoking unsynchronized methods on the object. Other threads can also use synchronized methods or the synchronized statement in a conventional manner to achieve mutual exclusion. The locks acquired by synchronized statements are the same as the locks that are acquired implicitly by synchronized methods; see §8.4.3.5. A single thread may hold a lock more than once. The example:
class Test {
public static void main(String[] args) {
Test t = new Test();
synchronized(t) {
synchronized(t) {
System.out.println("made it!");
}
}
}
}
prints: made it!
This example would deadlock if a single thread were not permitted to lock a lock more than once.
A synchronized method acquires a lock before it executes. For a class (static) method, the lock associated with the Class object for the method's class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used. These are the same locks that can be used by the synchronized statement; thus, the code:
class Test {
int count;
synchronized void bump() { count++; }
static int classCount;
static synchronized void classBump() {
classCount++;
}
}
has exactly the same effect as:
class BumpTest {
int count;
void bump() {
synchronized (this) {
count++;
}
}
static int classCount;
static void classBump() {
try {
synchronized (Class.forName("BumpTest")) {
classCount++;
}
} catch (ClassNotFoundException e) {
...
}
}
}
The more elaborate example:
public class Box {
private Object boxContents;
public synchronized Object get() {
Object contents = boxContents;
boxContents = null;
return contents;
}
public synchronized boolean put(Object contents) {
if (boxContents != null)
return false;
boxContents = contents;
return true;
}
}
defines a class which is designed for concurrent use. Each instance of the class Box has an instance variable contents that can hold a reference to any object. You can put an object in a Box by invoking put, which returns false if the box is already full. You can get something out of a Box by invoking get, which returns a null reference if the box is empty. If put and get were not synchronized, and two threads were executing methods for the same instance of Box at the same time, then the code could misbehave. It might, for example, lose track of an object because two invocations to put occurred at the same time.
|
你去www.java-cn.com看看
我接你的分了,谢谢!!
我接你的分了,谢谢!!
|
去www.cn-java.com看看吧
|
有分就要,管他是不是真的“专家分”
synchronized同步锁,只能有一个人进去,其他人在门外等着,等人家出来再进
synchronized同步锁,只能有一个人进去,其他人在门外等着,等人家出来再进
|
synchronized是一个同步关键字,应该是放哪里都行,想将那个方法同步就放在那个方法上,类,静态方法是一样的使用。
|
我是第一 呵呵
帖主国庆快乐噢!!
帖主国庆快乐噢!!
|
Up
|
接分~
|
国庆节快乐!
分 分 分 分
分 分 分 分 分分
分分分分 分 分 分 分 分分分分
分 分 分 分 分 分分分分
分 分 分 分 分 分分分分
分分 分分分分 分 分分分分分分分 分 分分
分分分 分 分 分 分 分分
分 分 分 分 分
分 分 分分 分 分 分 分分
分 分 分 分 分 分分
分分分 分分分分分分 分分 分分分
分 分 分 分
分 分 分 分 分分
分分分分 分 分 分 分 分分分分
分 分 分 分 分 分分分分
分 分 分 分 分 分分分分
分分 分分分分 分 分分分分分分分 分 分分
分分分 分 分 分 分 分分
分 分 分 分 分
分 分 分分 分 分 分 分分
分 分 分 分 分 分分
分分分 分分分分分分 分分 分分分
|
国庆节快乐
|
国庆快乐
|
up
|
国庆节快乐!
|
呵呵,接分
|
国庆节快乐!
|
国庆节我还要值班,在CSDN上到处接分。
|
国庆节快乐!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
happy
|
:)
|
接分
|
国庆节这么多人散分,朋友的决定真是英明,让我替他灌水
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。