我是这么理解多线程的 (二)

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
	}
}

 

 

同一对象,参数不同,方法不互斥

同一对象,参数相同,方法互斥

不同对象,参数不同,方法不互斥

不同对象,参数相同,方法互斥

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值