synchblock.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 36 行

JAVA
36
字号
public class SynchBlock implements Runnable
{
	static int myArray[];
	
	public void run()
	{
		try
		{
			Thread.sleep(500);
			System.out.println("new thread just woke up");
			synchronized(myArray)
			{
				for(int i=0; i < myArray.length; i++)
					System.out.println("inside run i="+i+" loation = " + myArray[i]);
			}
		}catch(InterruptedException e){}
	}

	public static void main(String args[]) throws InterruptedException
	{
		myArray = new int[3];
		Thread t = new Thread(new TrivialApplication() +"");
		t.start();
		synchronized(myArray)
		{
			Thread.sleep(2000);
			System.out.println("inside synch block");
			myArray[0] = 30;
			myArray[1] = 20;
			myArray[2] = 10;
			Thread.sleep(2000);
			System.out.println("about to leave synch block");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?