testrunnable.java

来自「针对越来越多的用户喜欢JAVA」· Java 代码 · 共 50 行

JAVA
50
字号
import java.applet .Applet ;
import java.awt .* ;
public class TestRunnable extends Applet implements Runnable 
{
	Label prompt1=new Label("第一个子线程");
	Label prompt2=new Label("第二个字线程");
	TextField threadFirst=new TextField (14);
	TextField threadSecond=new TextField (14);
	Thread thread1,thread2;
	int count1=0,count2;
        public void init()
	{
		add(prompt1);
		add(threadFirst);
		add(prompt2);
		add(threadSecond);
	}
	
	public void start()
	{
		thread1=new Thread(this,"FirstThread");
		thread2=new Thread(this,"SecondThread");
       		thread1.start();
		thread2.start();
	}
	public void run()
	{
		String curRunning;
		while(true)
		{
			try
			{
				Thread.sleep((int)(Math.random()*3000));
                 	}
			catch(InterruptedException e){}
			curRunning=Thread.currentThread ().getName();
			if(curRunning.equals ("FirstThread"))
			{
				count1++;
                                threadFirst.setText ("线程1第"+count1+"次被调度");
			}
			else if(curRunning.equals ("SecondThread"))
			{
				count2++;
				threadSecond.setText ("线程2第"+count2+"次被调度");
			}
		}   
	}
}

⌨️ 快捷键说明

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