📄 twothreads.java
字号:
import java.applet.Applet;import java.awt.*;import java.awt.event.*;public class TwoThreads extends Applet implements Runnable//Applet的主类{ Label prompt1=new Label("第一个子线程");//建立标签1 Label prompt2=new Label("第二个子线程");//建立标签2 TextField thread_1=new TextField(14);//建立文本框1 TextField thread_2=new TextField(14);//建立文本框2 Thread thread1,thread2;//声明两个线程对象 int count1=0,count2=0;//定义两个计数器 public void init() { add(prompt1); add(thread_1); add(prompt2); add(thread_2); } 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++; thread_1.setText("线程1第"+count1+"次被调度"); } else if(curRunning.equals("SecondThread")) { count2++; thread_2.setText("线程2第"+count2+"次被调度"); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -