c_6.java

来自「最简单的线程编程」· Java 代码 · 共 47 行

JAVA
47
字号
public class c_6{
  public static void main(String args[])
  {
  //创建线程以MyRunable的对象为目标对象
  Thread thread1=new Thread(new MyRunable(300),"线程1");
  Thread thread2=new Thread(new MyRunable(500),"线程2");
  Thread thread3=new Thread(new MyRunable(700),"线程3");

  thread1.start();
  thread2.start();
  thread3.start();

  //判断线程是否结束
  if(thread1.isAlive())
      System.out.println("线程1结束");

  if(thread2.isAlive())
        System.out.println("线程2结束");

  if(thread3.isAlive())
      System.out.println("线程3结束");
            }
            }

 class MyRunable implements Runnable{
 private int sleepTime;
 private String s;

 public MyRunable(int time)
 {
 sleepTime=time;
 }

 public void run()
 {
 try
 {for(int i=1;i<=3;i++)
 {
 Thread.sleep(sleepTime);
 s=Thread.currentThread().getName();
 System.out.println("线程:"+s);
 }
 }catch(InterruptedException e){}
 }
 }

⌨️ 快捷键说明

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