标签:new bubuko oid nbsp int ble string bsp rgs
1,继承 Thread
// 创建线程类 class MyThread extends Thread{ private String name; public MyThread(String name) { this.name = name; } // 每个线程循环输出 public void run() { for(int i = 0; i < 10; i++) { System.out.println(name + "------" + i); } } } // 测试类 public class Test1 { public static void main(String[] args) { MyThread t1 = new MyThread("线程A"); MyThread t2 = new MyThread("线程B"); MyThread t3 = new MyThread("线程C"); // 每个线程对象交替执行 t1.start(); t2.start(); t3.start(); } }
测试
2,实现 Runnable 接口
// 创建线程类 class MyThread implements Runnable{ private String name; public MyThread(String name) { this.name = name; } // 每个线程循环输出 public void run() { for(int i = 0; i < 10; i++) { System.out.println(name + "------" + i); } } } // 测试类 public class Test1 { public static void main(String[] args) { MyThread t1 = new MyThread("线程A"); MyThread t2 = new MyThread("线程B"); MyThread t3 = new MyThread("线程C"); // 实现接口必须要依靠 Thread 类 new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).start(); } }
标签:new bubuko oid nbsp int ble string bsp rgs
原文地址:https://www.cnblogs.com/huanggy/p/9523062.html