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()
	{
		for (int i = 1; i <= 10; i++)
		{
			System.out.print(chString);
		}
	}

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

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

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