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

📄 fivethreadtestrunnable.java

📁 一个可以动态演示JAVA中多线程的程序.
💻 JAVA
字号:
// <applet code=FiveThreadTestRunnable width=100 height=50></applet>

 import java.applet.Applet;//表明这是一个Applet程序
 import java.awt.*;//由于程序中使用了标签,文本框,按钮等组件

 public class FiveThreadTestRunnable extends Applet implements Runnable
 //Applet主类 继承类Applet并实现接口Runnable
{
  Label prompt1=new Label("第一个子线程");
  //建立标签1
  Label prompt2=new Label("第二个子线程");
  //建立标签2
  Label prompt3=new Label("第三个子线程");
  //建立标签3
  Label prompt4=new Label("第四个子线程");
  //建立标签4
  Label prompt5=new Label("第五个子线程");
  //建立标签5
  TextField threadFirst=new  TextField(14);
  //建立文本框1
  TextField threadSecond=new  TextField(14);
  //建立文本框2
  TextField threadThird=new  TextField(14);
  //建立文本框3
  TextField threadFourthly=new  TextField(14);
  //建立文本框4
  TextField threadFifthly=new  TextField(14);
  //建立文本框5
  Thread thread1,thread2,thread3,thread4,thread5;
  //声明五个Thread的线程对象
  int count1=0,count2,count3,count4,count5;
  //定义五个计数器
    public void init()
  //Applet程序的初始化方法
   {
     add(prompt1);
     add(threadFirst);
     add(prompt2);
     add(threadSecond);
     add(prompt3);
     add(threadThird);
     add(prompt4);
     add(threadFourthly);
     add(prompt5);
     add(threadFifthly);
     //分别将五个标签,文本框按照先后顺序加入Applet的界面
    }

    public void start()
   {
     thread1=new Thread(this,"FirstThread");
     thread2=new Thread(this,"SecondThread");
     thread3=new Thread(this,"ThirdThread");
     thread4=new Thread(this,"FourthlyThread");
     thread5=new Thread(this,"FifthlyThread");
     //创建五个线程对象,具有当前类的run()方法,并用字符串指定线程对象的名字
     thread1.start();  
     thread2.start();
     thread3.start();
     thread4.start();
     thread5.start();
     //启动线程对象,进入就绪状态
   }
    public void run()
    //定义Runnable接口的run()方法的具体内容
   {
      String curRunning;//定义一个字符串变量
      while(true)//进入一个无限循环中,循环永远中止
       {
         try 
         {
           Thread.sleep((int)(Math.random()*3000)); 
         }
         catch(InterruptedException e){}
         //调用Thread类的sleep()方法,使线程停止一段时间
         curRunning=Thread.currentThread().getName();
         //将当前线程对象的名字赋值给curRunning
         if(curRunning.equals ("FirstThread"))
         {
           count1++;
           threadFirst.setText("线程1第"+count1+"次被调度");
         }
         //调用String类的方法public boolen equals(Object anObject)来判断curRunning是否                
         //与"FirstThread"是否相等,如相等,相应计算器加1,并重置文本框内容.
         else if(curRunning.equals("SecondThread"))
         {
           count2++;
           threadSecond.setText("线程2第"+count2+"次被调度");
         }
         //调用String类的方法public boolen equals(Object anObject)来判断curRunning是否                
         //与"SecondThread"是否相等,如相等,相应计算器加1,并重置文本框内容.
         else if(curRunning.equals("ThirdThread"))
         {
           count3++;
           threadThird.setText("线程3第"+count3+"次被调度");
         }
         //调用String类的方法public boolen equals(Object anObject)来判断curRunning是否                
        //与"ThirdThread"是否相等,如相等,相应计算器加1,并重置文本框内容.
         else if(curRunning.equals("FourthlyThread"))
         {
           count4++;
           threadFourthly.setText("线程4第"+count4+"次被调度");
         }
         //调用String类的方法public boolen equals(Object anObject)来判断curRunning是否                
         //与"FourthlyThread"是否相等,如相等,相应计算器加1,并重置文本框内容.
         else if(curRunning.equals("FifthlyThread"))
         {
           count5++;
           threadFifthly.setText("线程5第"+count5+"次被调度");
         }
         //调用String类的方法public boolen equals(Object anObject)来判断curRunning是否                
         //与"FifthlyThread"是否相等,如相等,相应计算器加1,并重置文本框内容.
       }
        //while循环结束 
   }
    //run()方法结束
}

⌨️ 快捷键说明

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