Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
次の違いは何ですか:
public synchronized void test(){}
と
public void test() { synchronized(Sample.class){} }
違いをより明確にするために、最初のものは次のように書き直すことができます。
public void test() { synchronized(this){ } }
違いは、1 つ目はクラスのインスタンスで同期されるのに対し、2 つ目はクラス自体で同期されることです。
test()最初のケースでは、クラスの 2 つのインスタンスで2 つのスレッドを同時に実行できます。第二に、彼らはできません。
test()