スレッド同期ロックの問題

2053 ワード

public class TT implements Runnable {

	int b=100;
	public synchronized void m1() throws InterruptedException{
		System.out.println("m1 ");
		b=1000;
		Thread.sleep(5000);
		System.out.println("m1:"+b);
	}
	public synchronized void m2() throws InterruptedException{
		
		System.out.println("m2 ");
		Thread.sleep(2500);
		b=2000;
		System.out.println("m2:"+b);
	}
	public void run()  {
		System.out.println("run ");
		try {
			m1();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		TT tt=new TT();
		Thread t=new Thread(tt);
		t.start();
		
		//Thread.sleep(1);
		tt.m2();
		
		System.out.println("main:"+tt.b);
	}

}

m 2 run実行開始m 2:2000 main:2000 m 1実行開始m 1:1000
public class TT implements Runnable {

	int b=100;
	public synchronized void m1() throws InterruptedException{
		System.out.println("m1 ");
		b=1000;
		Thread.sleep(5000);
		System.out.println("m1:"+b);
	}
	public  void m2() throws InterruptedException{
		
		System.out.println("m2 ");
		Thread.sleep(2500);
		b=2000;
		System.out.println("m2:"+b);
	}
	public void run()  {
		System.out.println("run ");
		try {
			m1();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		TT tt=new TT();
		Thread t=new Thread(tt);
		t.start();
		
		//Thread.sleep(1);
		tt.m2();
		
		System.out.println("main:"+tt.b);
	}

}

m 2実行開始run実行開始m 1実行開始m 2:2000 main:2000 m 1:2000