线程同步(synchronized)

1)如果一个类有两个synchronized修饰的方法,如果有两个该类的对象分别执行这两个方法,每个方法中线程都适当随机休眠,那么结果会是如何:

package com.medivh.thread;

public class ThreadTest4
{
	public static void main(String[] args)
	{
		A a = new A();
		
		Thread t1 = new A1(a);
		
		a = new A();
		
		Thread t2 = new A2(a);
		
		t1.start();
		
		t2.start();
	}
}

class A
{
	public synchronized void test()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A1: " + i);
		}
	}
	
	public synchronized  void test2()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A2: " + i);
		}
	}
}


class A1 extends Thread
{
	private A a;
	
	public A1(A a)
	{	
		this.a = a;
	}
	
	public void run()
	{
		this.a.test();
	}
}

class A2 extends Thread
{
	private A a;
	
	public A2(A a)
	{	
		this.a = a;
	}
	
	@Override
	public void run()
	{
		this.a.test2();
	}
}



测试结果:

A2: 0
A1: 0
A2: 1
A1: 1
A2: 2
A2: 3
A2: 4
A1: 2
A2: 5
A1: 3
A2: 6
A2: 7
A2: 8
A1: 4
A2: 9
A1: 5
A1: 6
A1: 7
A1: 8
A1: 9


结果乱序,你休眠我运行,我休眠,你再运行。

2)如果上述方法中第一个方法无休眠操作,先执行第一个方法。同样是两个对象。

package com.medivh.thread;

public class ThreadTest4
{
	public static void main(String[] args)
	{
		A a = new A();
		
		Thread t1 = new A1(a);
		
		a = new A();
		
		Thread t2 = new A2(a);
		
		t1.start();
		
		t2.start();
	}
}

class A
{
	public synchronized void test()
	{
		for(int i = 0; i < 10; i++)
		{
//			try
//			{
//				Thread.sleep((long)(Math.random() * 1000));
//			}
//			catch (InterruptedException e)
//			{
//				e.printStackTrace();
//			}
			
			System.out.println("A1: " + i);
		}
	}
	
	public synchronized  void test2()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A2: " + i);
		}
	}
}


class A1 extends Thread
{
	private A a;
	
	public A1(A a)
	{	
		this.a = a;
	}
	
	public void run()
	{
		this.a.test();
	}
}

class A2 extends Thread
{
	private A a;
	
	public A2(A a)
	{	
		this.a = a;
	}
	
	@Override
	public void run()
	{
		this.a.test2();
	}
}



测试结果:

A1: 0
A1: 1
A1: 2
A1: 3
A1: 4
A1: 5
A1: 6
A1: 7
A1: 8
A1: 9
A2: 0
A2: 1
A2: 2
A2: 3
A2: 4
A2: 5
A2: 6
A2: 7
A2: 8
A2: 9


先执行我,我不休眠,你休想运行。

3)第一个方法休眠,第二个不休眠,依然先执行第一个方法,同样是两个对象。

package com.medivh.thread;

public class ThreadTest4
{
	public static void main(String[] args)
	{
		A a = new A();
		
		Thread t1 = new A1(a);
		
		a = new A();
		
		Thread t2 = new A2(a);
		
		t1.start();
		
		t2.start();
	}
}

class A
{
	public synchronized void test()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A1: " + i);
		}
	}
	
	public synchronized  void test2()
	{
		for(int i = 0; i < 10; i++)
		{
//			try
//			{
//				Thread.sleep((long)(Math.random() * 1000));
//			}
//			catch (InterruptedException e)
//			{
//				e.printStackTrace();
//			}
			
			System.out.println("A2: " + i);
		}
	}
}


class A1 extends Thread
{
	private A a;
	
	public A1(A a)
	{	
		this.a = a;
	}
	
	public void run()
	{
		this.a.test();
	}
}

class A2 extends Thread
{
	private A a;
	
	public A2(A a)
	{	
		this.a = a;
	}
	
	@Override
	public void run()
	{
		this.a.test2();
	}
}



运行结果:

A2: 0
A2: 1
A2: 2
A2: 3
A2: 4
A2: 5
A2: 6
A2: 7
A2: 8
A2: 9
A1: 0
A1: 1
A1: 2
A1: 3
A1: 4
A1: 5
A1: 6
A1: 7
A1: 8
A1: 9

不好意思,你刚运行还没出结果就休眠了,那么轮到我了,我不休眠,打印完所有结果,你再继续吧。
4)两个方法都有休眠,但是只有一个对象。

package com.medivh.thread;

public class ThreadTest4
{
	public static void main(String[] args)
	{
		A a = new A();
		
		Thread t1 = new A1(a);
		
		//a = new A();
		
		Thread t2 = new A2(a);
		
		t1.start();
		
		t2.start();
	}
}

class A
{
	public synchronized void test()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A1: " + i);
		}
	}
	
	public synchronized  void test2()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A2: " + i);
		}
	}
}


class A1 extends Thread
{
	private A a;
	
	public A1(A a)
	{	
		this.a = a;
	}
	
	public void run()
	{
		this.a.test();
	}
}

class A2 extends Thread
{
	private A a;
	
	public A2(A a)
	{	
		this.a = a;
	}
	
	@Override
	public void run()
	{
		this.a.test2();
	}
}



程序结果:

A1: 0
A1: 1
A1: 2
A1: 3
A1: 4
A1: 5
A1: 6
A1: 7
A1: 8
A1: 9
A2: 0
A2: 1
A2: 2
A2: 3
A2: 4
A2: 5
A2: 6
A2: 7
A2: 8
A2: 9

synchronized老板说我必须先干完一件事再去干另外一件,不好意思,我不能分身啊。
5)两个方法都休眠,蛋第一个方法为静态的

package com.medivh.thread;

public class ThreadTest4
{
	public static void main(String[] args)
	{
		A a = new A();
		
		Thread t1 = new A1(a);
		
		//a = new A();
		
		Thread t2 = new A2(a);
		
		t1.start();
		
		t2.start();
	}
}

class A
{
	public synchronized static void test()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A1: " + i);
		}
	}
	
	public synchronized  void test2()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A2: " + i);
		}
	}
}


class A1 extends Thread
{
	private A a;
	
	public A1(A a)
	{	
		this.a = a;
	}
	
	public void run()
	{
		A.test();//this.a.test();
	}
}

class A2 extends Thread
{
	private A a;
	
	public A2(A a)
	{	
		this.a = a;
	}
	
	@Override
	public void run()
	{
		this.a.test2();
	}
}



运行结果:

A2: 0
A1: 0
A1: 1
A2: 1
A2: 2
A1: 2
A2: 3
A2: 4
A1: 3
A2: 5
A1: 4
A1: 5
A1: 6
A2: 6
A2: 7
A1: 7
A1: 8
A2: 8
A2: 9
A1: 9


对不起,我有这个方法,但是我领导现在要直接控制我的那个权力,我还是管好我自己能管得,那个权力,领导爱干嘛干嘛。

6)两个方法都是静态的

package com.medivh.thread;

public class ThreadTest4
{
	public static void main(String[] args)
	{
		A a = new A();
		
		Thread t1 = new A1(a);
		
		//a = new A();
		
		Thread t2 = new A2(a);
		
		t1.start();
		
		t2.start();
	}
}

class A
{
	public synchronized static void test()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A1: " + i);
		}
	}
	
	public synchronized static  void test2()
	{
		for(int i = 0; i < 10; i++)
		{
			try
			{
				Thread.sleep((long)(Math.random() * 1000));
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			System.out.println("A2: " + i);
		}
	}
}


class A1 extends Thread
{
	private A a;
	
	public A1(A a)
	{	
		this.a = a;
	}
	
	public void run()
	{
		A.test();//this.a.test();
	}
}

class A2 extends Thread
{
	private A a;
	
	public A2(A a)
	{	
		this.a = a;
	}
	
	@Override
	public void run()
	{
		A.test2();//this.a.test2();
	}
}



运行结果:

A1: 0
A1: 1
A1: 2
A1: 3
A1: 4
A1: 5
A1: 6
A1: 7
A1: 8
A1: 9
A2: 0
A2: 1
A2: 2
A2: 3
A2: 4
A2: 5
A2: 6
A2: 7
A2: 8
A2: 9

好吧,好吧,我被革职了,我的领导现在在管理我以前的东西。这些都是别人的,而且属于同一个人的。

原文地址:https://www.cnblogs.com/MedivhQ/p/4074962.html