同步块有什么作用? [英] What do synchronized block?

查看:141
本文介绍了同步块有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下课程

  class  Class1 {

public void Method1(){
synchronized (myobject){
/ * 一些代码* /
}
}
}



其中myobject是该类的实例

 < span class =code-keyword> class  myClass {

public void Method2(){
synchronized (someOtherObj){
/ * 一些代码* /
}
}

public synchronized void Method3(){
/ * 一些没有同步块的代码* /
}

public void Method4( ){
/ * 一些没有同步块的代码* /
}
}



请帮助我理解myobject的哪些代码块仅适用于方法Method1。

解决方案

您可以在原始文档中阅读它( http:// docs。 oracle.com/javase/tutorial/essential/concurrency/sync.html [ ^ ]),但了解背景会更好首先。这可以提供更多帮助,请尝试彻底了解它:

http://en.wikipedia.org/ wiki / Mutual_exclusion [ ^ ]。



Java(和其他系统)互斥的一个重要特征是同步是由某个对象完成的。这意味着如果两个不同的线程执行由不同对象保护的某些代码片段,则不会发生同步,任何线程都不会等待另一个。关于这个的基本原理很容易解释为这样一个简单的共享资源的例子,作为内存中的一些对象集。假设您有一些对象集,您需要保持完整性。如果您通过其他某个线程访问它,它可能会被破坏,因为一个线程会修改它的一半,但另一个线程会假设保留完整性,这可能会导致不可预测的结果。但是如果两个不同的共享对象集是不相关的,那么很明显一个线程可以安全地修改一个,而任何其他线程在另一个线程上工作。因此,每组共享对象都可以由单独的同步对象保护。如果分析性能考虑因素,您会发现它涉及的耦合较少,并且吞吐量要好得多。 (常见的线程智慧说:最佳同步不同步。)



由于同步机制,当一个线程开始执行同步的代码片段而另一些线程已经在执行它,第二个线程将被放入等待队列(总是需要队列,因为多个线程可能正在等待)并进入休眠状态,不会浪费任何CPU时间,直到它被唤醒 。这个等待线程被称为处于等待状态:它被关闭并且没有被调度回执行,直到它被唤醒。其中一个唤醒事件是由执行守卫的代码片段的线程释放锁定,当该线程退出同步的上下文时发生。



-SA


对象上的同步块使该部分成为线程安全的。意味着一次只有一个线程可以访问该块。要访问该块线程必须获取锁定然后它可以进行操作&该块内的变化。


简而言之,它实现了互斥。 >

Suppose we have the following class

class Class1 {

    public void Method1() {
        synchronized(myobject) {
            /* some code */
        }
    }
}


where myobject is the instance of the class

class myClass {

     public void Method2() {
        synchronized(someOtherObj) {
            /* some code */
        }
     }

     public synchronized void Method3() {
        /* some code without synchronized blocks */
     }

     public void Method4() {
        /* some code without synchronized blocks */
     }
}


Help me please to understand which code blocks of myobject are made available only for the tread called the method Method1.

解决方案

You could read about it in original documentation (http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html[^]), but it would be better to understand the background first. This can help more, please try to understand it thoroughly:
http://en.wikipedia.org/wiki/Mutual_exclusion[^].

The big essential feature of Java (and other systems') mutual exclusion is that the synchronization is done by some object. It means that if two different threads execute some fragments of code "guarded" by different objects, no synchronization happens, none of the thread won't wait for another one. The rationale on this is easy to explain on the example of such a simple shared resource as some set of objects in memory. Suppose you have some object set you need to keep in integrity. If you access it by some other thread, it can be broken, because one thread modifies "half of it", but another thread works in assumption that the integrity is preserved, which could lead to unpredictable results. But if some two different shared sets of objects are unrelated, it's apparent that one thread can safely modify one while any other thread is working at another one. So, each set of shared objects can be guarded by a separate synchronization object. If you analyze performance considerations, you could see that it involves less coupling and is much better for throughput. (Common threading wisdom says: "best synchronization is no synchronization".)

Due to synchronization mechanism, when a thread starts to execute a synchronized fragment of code while some other thread is already executing it, this second thread will be put to a wait queue (the queue is always needed, because more than one thread might be waiting) and put to sleep, not wasting any CPU time, until it is "waken up". This waiting thread is said to be put in a "wait state": it is switched off from and not scheduled back to execution until it is waken up. One of the waking event is releasing the lock by the thread executing the guarded fragment of code, which happens when this thread goes out of the synchronized context.

—SA


Synchronized Blocks on object make that part thread-safe. Means Only one thread at a time can access that block. To access that block thread have to acquire lock then it can make operations & changes inside that block.

In short it implements the mutual exclusion.


这篇关于同步块有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆