java中线程同步块synchronized的实现二

package cn.com.secn.thead;

class MyThead extends Thread
{
	private String chString;
	static Object printerObject = new Object();

	public MyThead(String ch)
	{
		chString = ch;
	}

	public void intercept()
	{
		synchronized (printerObject)
		{
			for (int i = 1; i <= 10; i++)
			{
				System.out.print(chString);
			}
		}
	}

	public void run()
	{

		for (int i = 1; i <= 5; i++)
		{
			intercept();
			System.out.println();
		}

	}
}

public class TheadMain
{
	public static void main(String[] args)
	{
		MyThead thread1 = new MyThead("A");
		MyThead thread2 = new MyThead("B");
		MyThead thread3 = new MyThead("C");
		thread1.start();
		thread2.start();
		thread3.start();
	}
}

你可能感兴趣的:(java中线程同步块synchronized的实现二)