producer.java

来自「线程实例」· Java 代码 · 共 34 行

JAVA
34
字号
import javax.swing.*;

public class Producer extends Thread
{
	private Buffer sharedLocation;
	private JTextArea outputArea;
	
	public Producer(Buffer shared,JTextArea output)
	{
		super("Producer");
		sharedLocation=shared;
		outputArea=output;
	}
	
	public void run()
	{
		for(int i=11;i<=20;i++)
		{
			try
			{
				Thread.sleep((int)(Math.random()*3000));
				sharedLocation.set(i);
			}
			catch(InterruptedException exception)
			{
				exception.printStackTrace();
			}
		}
		String name=getName();
		SwingUtilities.invokeLater(new RunnableOutput(outputArea,"\n"+name+" done producing.\n"+
									name+" terminated.\n"));
	}
}

⌨️ 快捷键说明

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