package test2;
public class MainService {
private static MainService instance = new MainService();
public static MainService getInstance() {
return instance;
}
@SuppressWarnings("static-access")
public void fun(String parm) {
synchronized (parm) {
for (int i = 1; i < 4; i++) {
StringBuffer buf = new StringBuffer();
buf.append("当前对象:").append(this).append("\t\t");
buf.append("当前线程:").append(Thread.currentThread().getName())
.append("\t\t");
buf.append("当前i值:").append(i);
System.out.println(buf.toString());
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
package test2;
public class TestThread {
public static final String str01 = "1";
public static final String str02 = "2";
public static void test01() {
new Thread(new Runnable() {
@Override
public void run() {
MainService.getInstance().fun(str01);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
MainService.getInstance().fun(str02);
}
}).start();
}
public static void test02() {
new Thread(new Runnable() {
@Override
public void run() {
MainService.getInstance().fun(str01);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
MainService.getInstance().fun(str01);
}
}).start();
}
public static void test03() {
new Thread(new Runnable() {
@Override
public void run() {
new MainService().fun(str01);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
new MainService().fun(str02);
}
}).start();
}
public static void test04() {
new Thread(new Runnable() {
@Override
public void run() {
new MainService().fun(str01);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
new MainService().fun(str01);
}
}).start();
}
public static void main(String[] args) {
// test01();
// 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:1
// 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:1
// 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:2
// 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:2
// 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:3
// 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:3
// test02();
// 当前对象:test.MainService@41d5550d 当前线程:Thread-0 当前i值:1
// 当前对象:test.MainService@41d5550d 当前线程:Thread-0 当前i值:2
// 当前对象:test.MainService@41d5550d 当前线程:Thread-0 当前i值:3
// 当前对象:test.MainService@41d5550d 当前线程:Thread-1 当前i值:1
// 当前对象:test.MainService@41d5550d 当前线程:Thread-1 当前i值:2
// 当前对象:test.MainService@41d5550d 当前线程:Thread-1 当前i值:3
// test03();
// 当前对象:test2.MainService@24c21495 当前线程:Thread-0 当前i值:1
// 当前对象:test2.MainService@41d5550d 当前线程:Thread-1 当前i值:1
// 当前对象:test2.MainService@24c21495 当前线程:Thread-0 当前i值:2
// 当前对象:test2.MainService@41d5550d 当前线程:Thread-1 当前i值:2
// 当前对象:test2.MainService@41d5550d 当前线程:Thread-1 当前i值:3
// 当前对象:test2.MainService@24c21495 当前线程:Thread-0 当前i值:3
// test04();
// 当前对象:test2.MainService@24c21495 当前线程:Thread-0 当前i值:1
// 当前对象:test2.MainService@24c21495 当前线程:Thread-0 当前i值:2
// 当前对象:test2.MainService@24c21495 当前线程:Thread-0 当前i值:3
// 当前对象:test2.MainService@41d5550d 当前线程:Thread-1 当前i值:1
// 当前对象:test2.MainService@41d5550d 当前线程:Thread-1 当前i值:2
// 当前对象:test2.MainService@41d5550d 当前线程:Thread-1 当前i值:3
}
}
同一对象,参数不同,方法不互斥
同一对象,参数相同,方法互斥
不同对象,参数不同,方法不互斥
不同对象,参数相同,方法互斥