package com.tedu.threadStudy;
public class studyThread {
public static void main(String[] args) {
MyThread th1 = new MyThread("线程1");
MyThread th2 = new MyThread("线程2");
th1.start();
th2.start();
}
}
class MyThread extends Thread{
private final String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(name+"线程执行了");
}
}
}
通过继承接口的方式实现多线程
package com.tedu.threadStudy;
public class studyRunnable {
public static void main(String[] args) {
MyThread2 th1 = new MyThread2("线程一");
MyThread2 th2 = new MyThread2("线程二");
// 通过接口实现,必须通过类重新实例化
Thread tha = new Thread(th1, "1");
Thread thb = new Thread(th2, "2");
tha.start();
thb.start();
}
}
class MyThread2 implements Runnable{
private String name;
public MyThread2(String name){
this.name = name;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(name+"线程执行了");
}
}
}