小弟我是这么理解多线程的 (一)
我是这么理解多线程的 (一)
package test2; public class MainService { private static MainService instance = new MainService(); public static MainService getInstance() { return instance; } /** * 同步块 synchronized (this) * @param parm */ @SuppressWarnings("static-access") public void fun03(String parm) { synchronized (this) { 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(); } } } } /** * 同步块 synchronized (this.getClass()) * @param parm */ @SuppressWarnings("static-access") public void fun04(String parm) { synchronized (this.getClass()) { 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(); } } } } }
一个使用同步块 synchronized (this)
另一个使用同步块 synchronized (this.getClass())
测试类,TestThread03
package test2; public class TestThread03 { public static void test01() { new Thread(new Runnable() { @Override public void run() { new MainService().fun03(null); } }).start(); new Thread(new Runnable() { @Override public void run() { new MainService().fun03(null); } }).start(); } public static void test02() { new Thread(new Runnable() { @Override public void run() { MainService.getInstance().fun03(null); } }).start(); new Thread(new Runnable() { @Override public void run() { MainService.getInstance().fun03(null); } }).start(); } public static void main(String[] args) { // test01(); // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:1 // 当前对象:test.MainService@41d5550d 当前线程:Thread-1 当前i值:1 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:2 // 当前对象:test.MainService@41d5550d 当前线程:Thread-1 当前i值:2 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:3 // 当前对象:test.MainService@41d5550d 当前线程:Thread-1 当前i值:3 // test02(); // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:1 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:2 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:3 // 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:1 // 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:2 // 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:3 } }
结果:
不同对象执行同一方法,方法不互斥。
同一对象执行同一方法,方法互斥。
package test2; public class TestThread04 { public static void test01() { new Thread(new Runnable() { @Override public void run() { new MainService().fun04(null); } }).start(); new Thread(new Runnable() { @Override public void run() { new MainService().fun04(null); } }).start(); } public static void test02() { new Thread(new Runnable() { @Override public void run() { MainService.getInstance().fun04(null); } }).start(); new Thread(new Runnable() { @Override public void run() { MainService.getInstance().fun04(null); } }).start(); } public static void main(String[] args) { // test01(); // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:1 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:2 // 当前对象:test.MainService@24c21495 当前线程: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 // test02(); // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:1 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:2 // 当前对象:test.MainService@24c21495 当前线程:Thread-0 当前i值:3 // 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:1 // 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:2 // 当前对象:test.MainService@24c21495 当前线程:Thread-1 当前i值:3 } }
结果:
同一类的所有对象执行同一方法,均互斥。