java 에서 synchronized 에 대한 일반적인 용법
40372 단어 synchronized
package j2se.thread.test;
/***
* synchronized(class) , class monitor .
* class this , class, class .
, :
synchronized(class)
synchronized(this)
-> monitor, .
synchronized(this)
synchronized(this)
-> , , , .
synchronized(class)
synchronized(class)
-> , .
*/
/**
* @author liwei
synchronized(this) synchronized(class)
*/
public class TestSynchronied8 {
private static byte[] lockStatic = new byte[0]; // instance
private byte[] lock = new byte[0]; // instance
public synchronized void m4t5() {
System.out.println(this);
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
public void m4t0() {
synchronized(this) {
System.out.println(this);
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
}
public void m4t1() {
synchronized(lock) {
System.out.println(this);
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
}
/**
* synchronized(class)
synchronized(class)
-> , .
*/
public static synchronized void m4t2() {
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
public static void m4t3() {
synchronized(TestSynchronied8.class){
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
}
public static void m4t4() {
synchronized(lockStatic){
int i = 50;
while( i-- > 0) {
System.out.println(Thread.currentThread().getName() + " : " + i);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
Thread.yield();
}
}
}
/**
synchronized(this)
synchronized(this)
-> , .
*/
public static void testObjsyn1(){
final TestSynchronied8 myt2 = new TestSynchronied8();
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println(" , synchronized(this) -------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t1 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t1" );
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t0(); } }, "t2" );
t1.start();
t2.start();
}
/**
synchronized(this)
synchronized(this)
-> , .
*/
public static void testObjsyn2(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println(" , synchronized(this) -------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t3 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t3" );
Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t4" );
t3.start();
t4.start();
}
/**
synchronized(this)
synchronized(this)
-> , .
*/
public static void testObjsyn3(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println(" , synchronized(obj) -------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t5 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t5" );
Thread t6 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t6" );
t5.start();
t6.start();
}
/**
synchronized(this)
synchronized(this)
-> , .
*/
public static void testObjsyn4(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
System.out.println(" , synchronized -------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t8" );
t7.start();
t8.start();
}
/**
synchronized(this)
synchronized(this)
-> , .
*/
public static void testObjsyn5(){
final TestSynchronied8 myt1 = new TestSynchronied8();
final TestSynchronied8 myt2 = new TestSynchronied8();
try {
System.out.println(" , synchronized -------------------------------------");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt2.m4t5(); } }, "t10" );
t9.start();
t10.start();
}
/**
synchronized(this)
synchronized
-> , syschronized(this), , .
*/
public static void testObjsyn6(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t10" );
t9.start();
t10.start();
}
/**
synchronized(lock)
synchronized
-> , syschronized(lock), , .
*/
public static void testObjsyn7(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t5(); } }, "t10" );
t9.start();
t10.start();
}
/**
synchronized(lock)
synchronized
-> , syschronized(lock), syschronized(this), .
*/
public static void testObjsyn71(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t9 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t9" );
Thread t10 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t10" );
t9.start();
t10.start();
}
/**
* synchronized(class)
synchronized(class)
-> , .
*/
public static void testObjsyn8(){
final TestSynchronied8 myt1 = new TestSynchronied8();
final TestSynchronied8 myt2 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t2 = new Thread( new Runnable() { public void run() { myt2.m4t2(); } }, "t2" );
Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t4" );
t2.start();
t4.start();
}
/**
* synchronized(class)
synchronized(class)
-> , .
*/
public static void testObjsyn9(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t2 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t2" );
Thread t4 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t4" );
t2.start();
t4.start();
}
/**
* synchronized(class)
synchronized(this)
-> monitor, .
*/
public static void testObjsyn10(){
final TestSynchronied8 myt1 = new TestSynchronied8();
final TestSynchronied8 myt2 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt2.m4t0(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized(class)
synchronized(this)
-> monitor, .
*/
public static void testObjsyn11(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t0(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized(class)
synchronized(lock)
-> monitor, .
*/
public static void testObjsyn12(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized(class)
synchronized(lockStatic)
-> monitor, .
*/
public static void testObjsyn13(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized
synchronized(lockStatic)
-> monitor, .
*/
public static void testObjsyn14(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized
synchronized(lockStatic)
-> monitor, .
*/
public static void testObjsyn15(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t3(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t2(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized
synchronized(lockStatic)
-> monitor, .
*/
public static void testObjsyn16(){
final TestSynchronied8 myt1 = new TestSynchronied8();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread t7 = new Thread( new Runnable() { public void run() { myt1.m4t1(); } }, "t7" );
Thread t8 = new Thread( new Runnable() { public void run() { myt1.m4t4(); } }, "t8" );
t7.start();
t8.start();
}
/**
* synchronized(class) static synchronized
* synchronized(class) synchronized(lockStatic)
* synchronized(this) synchronized
* ( ) synchronized(this) ( ) synchronized(lock)
*
* synchronized(class) static synchronized
* synchronized(this) synchronized
* synchronized(lockStatic)
* synchronized(lock)
* , ;
*/
public static void main(String[] args) {
// , synchronized(this)
// , synchronized(lock) lock
// testObjsyn1();// , . synchronized(this)
// testObjsyn2();// , . synchronized(this)
// testObjsyn3();// , . synchronized(lock)
// testObjsyn4();// , . synchronized
// testObjsyn5();// , . synchronized
// testObjsyn6();// , syschronized(this), ,
// testObjsyn7(); // , syschronized(lock), , ,
// testObjsyn71(); // , syschronized(lock), syschronized(this), ,
/**
* synchronized(class)
synchronized(class)
-> , .
*/
// testObjsyn8();// , .synchronized(class)
// testObjsyn9();// , .synchronized(class)
// testObjsyn10();// , .synchronized(class),synchronized(this)
// testObjsyn11();// , .synchronized(class),synchronized(this)
// testObjsyn12();// , .synchronized(class),synchronized(lock)
// testObjsyn13();// , .synchronized(class),synchronized(lockStatic)
// testObjsyn14();// , .synchronized() ,synchronized(lockStatic)
// testObjsyn15();// , .synchronized() ,synchronized(class)
// testObjsyn16();// , .synchronized(lock) ,synchronized(lockStatic)
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
javasynchronized 동기화 정적 방법과 동기화 비정적 방법의 공통점우리는synchronized를 사용하여 대상 변수를 동기화할 수 있을 뿐만 아니라,synchronizedl를 통해 동기화 클래스의 정적 방법과 비정적 방법을 동기화할 수 있습니다. 자바 관련 문법에서 알 수 있듯이s...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.