java Runnable Demo

From , 5 Years ago, written in Java, viewed 200 times.
URL https://pastebin.vip/view/c3c59e5f
  1. public class Test implements Runnable {
  2.         private String name;
  3.  
  4.         public Test(String name) {
  5.                 this.name = name;
  6.         }
  7.  
  8.         public void run() {
  9.                 for (int i = 0; i < 100; i++) {
  10.                         System.out.println("线程开始:" + this.name + ",i=" + i);
  11.                         try {
  12.                                 Thread.sleep((long) (Math.random() * 600)); // 睡眠时间随机
  13.                         } catch (InterruptedException e) {
  14.                                 // TODO Auto-generated catch block
  15.                                 e.printStackTrace();
  16.                         }
  17.                 }
  18.         }
  19.  
  20.         public static void main(String[] args) {
  21.                 Test t1 = new Test("线程1");
  22.                 Test t2 = new Test("线程2");
  23.                 Test t3 = new Test("线程3");
  24.  
  25.                 // 实现Runnable接口的子类不像继承Thread类的子类那样有start()方法,可以通过Thread类来启动Runnable线程
  26.                 new Thread(t1).start();
  27.                 new Thread(t2).start();
  28.                 new Thread(t3).start();
  29.  
  30.         }
  31. }

Reply to "java Runnable Demo"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website