我需要一些帮助,以确保我理解同步块。假设下面的例子:Java线程:同步块
public class ThreadStarter {
public static void main(String[] args) {
Queue queueObject = new Queue();
ThreadA thread1 = new ThreadA(queueObject);
ThreadA thread2 = new ThreadA(queueObject);
ThreadB thread3 = new ThreadB(queueObject);
ThreadB thread4 = new ThreadB(queueObject);
thread1.start();
thread2.start();
}
}
public class Queue {
Object[] the theQueue;
public Queue(int size){
theQueue = new Object[size];
}
public submitObject(Object o){
/* add Object to the queue */
}
public deleteObject(int objectId){
/*delete Object from the queue */
}
}
public class ThreadA extends Thread {
private Queue queue;
public ThreadA(Queue queue){
this.queue = queue;
}
public void run() {
while(!isInterrupted()){
synchronized(queue) {
Object o = new Object
queue.submitObject(o);
/* do some other stuff */
}
try {
sleep((int)(Math.random()*1000));
} catch (interruptedException) {
Thread.currentThread().interrupt;
}
synchronized(queue) {
/* do some other stuff on the queue */
}
}
}
}
public class ThreadB extends Thread {
private Queue queue;
public ThreadB(Queue queue){
this.queue = queue;
}
public void run() {
while(!isInterrupted()){
synchronized(queue) {
queue.deleteObject(o);
/* do some other stuff */
}
try {
sleep(1000);
} catch (interruptedException) {
Thread.currentThread().interrupt;
}
}
}
}
我的问题是,它足以同步ThreadA中的整个队列对象提交的目标队列类,是在安全方面?我在ThreadB中做了同样的操作,从队列中删除一个对象。或者,我还必须同步Queue类中的submitObject()和deleteObject()方法吗?
在我的理解中,如果我像上面显示的那样锁定了线程中的整个队列类,我应该保持安全 - 对吧?
greetZ并提前致谢。
+1:你封装同步的越多,它越容易和安全。 – 2012-03-12 19:43:39
它在功能上并不等同于“队列”的方法。如果他这样做了,那么在每个'synchronized'部分找到的'/ *'还有一些其他的东西* /'不会处于关键部分并且可以并行运行。根据代码的内容,这对同步也很重要。这与同步列表很相似:如果你想以原子方式执行多个操作(例如遍历它),你仍然需要在列表上进行外部同步。 – 2012-03-12 20:00:58
@MaxPeters完全同意。 – assylias 2012-03-12 20:02:12