问题总结之多线程

今天跟同事讨论了一下多线程并发问题,为了验证自己的想法,写了如下代码:
package com.zken.pkg;

public class MultiThread implements Runnable {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void run() {
		if (this.getName().equals("1")) {
			m1();
		}else if(this.getName().equals("2")){
			m2();
		}
	}

	private synchronized void m1() {
		try {
			Thread.sleep(1000 * 10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("m1::::"+this.getName());
	}

	private void m2() {
		System.out.println("m2::::"+this.getName());
	}

	public static void main(String[] args) {
		MultiThread multiThread = new MultiThread();
		Thread thread1 = new Thread(multiThread);
		multiThread.setName("1");
		thread1.start();
		Thread thread2 = new Thread(multiThread);
		multiThread.setName("2");
		thread2.start();
	}
}

我预测的结果是:

m2::::2
m1::::2

实际结果:

m2::::2
m2::::2


还请南来北往的神牛们帮忙解释下哈~~~~~~~~~~~~~~~~~~

你可能感兴趣的:(问题总结之多线程)