通过Thread 实现的线程
public class Demo1 {
public static void main(String args[]) {
Thread thread = Thread.currentThread();
System.out.println("当前线程:" + thread);
thread.setName("hyh thread");//修改线程名称
System.out.println("修改名称之后:" + thread);
try {
for (int a = 5; a > 0; a--) {
System.out.println(a);
thread.sleep(1000);
}
} catch (Exception e) {
System.out.println("出现异常");
}
}
三个线程的实现
在日常生产中,使用线程可以通过实现Runnable接口.下面我们通过该方法实现简单的三个线程Demo
结构: 创建一个Demo4类,另外一个主类MultThreadDemo.
清单:
public class Demo4 implements Runnable {
//全局变量
String name;
Thread thread;
//构造器
public Demo4(String th) {
name = th;
thread = new Thread(this, name);
System.out.println("新线程" + thread);
//开始线程
thread.start();
}
//重写run方法
public void run() {
try {
for (int a = 5; a > 0; a--) {
System.out.println(name + ":" + a);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println("异常");
}
System.out.println(name + "线程结束");
}
}
/**
* 测试类
*/
class MultThreadDemo {
public static void main(String[] args) {
//创建三个线程
Demo4 thread_1 = new Demo4("线程一");
Demo4 thread_2 = new Demo4("线程二");
Demo4 thread_3 = new Demo4("线程三");
//查看状态
System.out.println("线程一状态:" + thread_1.thread.isAlive());
System.out.println("线程二状态:" + thread_2.thread.isAlive());
System.out.println("线程三状态:" + thread_3.thread.isAlive());
try {
System.out.println("等待其他线程结束");
//使用join确保主线程最后运行
thread_1.thread.join();
thread_2.thread.join();
thread_3.thread.join();
} catch (InterruptedException e) {
System.out.println("线程异常");
}
//查看状态
System.out.println("线程一:" + thread_1.thread.isAlive());
System.out.println("线程二:" + thread_2.thread.isAlive());
System.out.println("线程三:" + thread_3.thread.isAlive());
}
}