1 定义类
public class Function {
private static synchronized void staticFunction() { // 静态方法
for (int i = 0; i < 4; i++) {
try {
System.out.println(Thread.currentThread().getName() + "---" + i);
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private synchronized void ordinaryFunction() { // 普通方法
for (int i = 0; i < 4; i++) {
try {
System.out.println(Thread.currentThread().getName() + "---" + i);
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void localFunction(String key) {
synchronized (key) {
for (int i = 0; i < 4; i++) {
try {
System.out.println(Thread.currentThread().getName() + "---" + key + "---" + i);
Thread.sleep(100L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
2 局部代码块: “sameKey”: 有效; new String(“sameKey”): 失效
@Test
public void localTest() {
System.out.println("----------localTest---sameKey----------");
Function function = new Function();
for (int i = 0; i < 2; i++) {
new Thread(() -> function.localFunction("sameKey")).start();
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("----------localTest---new String('sameKey')----------");
for (int i = 0; i < 2; i++) {
new Thread(() -> function.localFunction(new String("sameKey"))).start();
}
try {
Thread.sleep(100000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
----------localTest---sameKey----------
Thread-1---sameKey---0
Thread-1---sameKey---1
Thread-1---sameKey---2
Thread-1---sameKey---3
Thread-0---sameKey---0
Thread-0---sameKey---1
Thread-0---sameKey---2
Thread-0---sameKey---3
----------localTest---new String('sameKey')----------
Thread-2---sameKey---0
Thread-3---sameKey---0
Thread-2---sameKey---1
Thread-3---sameKey---1
Thread-2---sameKey---2
Thread-3---sameKey---2
Thread-2---sameKey---3
Thread-3---sameKey---3
3 常规方法
@Test
public void ordinaryTest(){
System.out.println("----------ordinaryTest---sameFunction----------");
Function function = new Function();
Function function2 = new Function();
for (int i = 0; i < 2; i++) {
new Thread(() -> function.ordinaryFunction()).start();
}
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("----------ordinaryTest---differentFunction----------");
new Thread(() -> function.ordinaryFunction()).start();
new Thread(() -> function2.ordinaryFunction()).start();
try {
Thread.sleep(100000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
----------ordinaryTest---sameFunction----------
Thread-0---0
Thread-0---1
Thread-0---2
Thread-0---3
Thread-1---0
Thread-1---1
Thread-1---2
Thread-1---3
----------ordinaryTest---differentFunction----------
Thread-2---0
Thread-3---0
Thread-2---1
Thread-3---1
Thread-2---2
Thread-3---2
Thread-2---3
Thread-3---3
4 静态方法
@Test
public void staticTest() {
System.out.println("----------staticTest---staticFunction----------");
for (int i = 0; i < 2; i++) {
new Thread(() -> Function.staticFunction()).start();
}
try {
Thread.sleep(100000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
----------staticTest---staticFunction----------
Thread-1---0
Thread-1---1
Thread-1---2
Thread-1---3
Thread-0---0
Thread-0---1
Thread-0---2
Thread-0---3