⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userunnable.java

📁 UseRunnable的JAVA小程序
💻 JAVA
字号:
//:UseRunnable.java
import java.applet.Applet;
import java.awt.*;

public class UseRunnable extends Applet implements Runnable {
	Label prompt1=new Label("first Thread");
	Label prompt2=new Label("second Thread");	
	TextField threadFirst=new TextField(14);
	TextField threadSecond=new TextField(14);
	Thread thread1,thread2;
	int count1=0,count2=0;
	
	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 stop(){
		if (thread1.isAlive()) thread1.stop();
		if (thread2.isAlive()) thread2.stop();
	}
	
	public void run() {
		String currentRunning;
		
		while (true) {
			try {
				Thread.sleep((int)(Math.random()*30000));
		    }
		    catch (InterruptedException e) {
		    }
		
			currentRunning=Thread.currentThread().getName();
			if (currentRunning.equals("FirstThread")) {
				count1++;
				threadFirst.setText(count1);
			}
			else if (currentRunning.equals("SecondThread")) {
				count2++;
				threadSecond.setText(count2);
			}
		
		}//end of the while
	}//end of the run()
}///:~

⌨️ 快捷键说明

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