线程周期
JAVA的线程实现有2种方式:
方式一:继承Thread类
- 定义一个MyThread类继承Thread类
- 在MyThread类中重写run()方法
- 创建MyThread类的对象
- 启动线程
基于方式一列出以下三种控制方式
1、Thread.sleep
使当前正在执行的线程暂停制定的毫秒数
线程类:
package Thread;
public class Mythread extends Thread{
public Mythread() {
}
public Mythread(String name) {
//supper将参数指向父类的参数构造方法
super(name);
}
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(getName()+">>>>>>"+i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
测试类:
package Thread;
public class Mythreaddemo1 {
public static void main(String[] args) {
Mythread mythread1 = new Mythread();
Mythread mythread2 = new Mythread();
mythread1.setName("高铁");
mythread2.setName("飞机");
//设置线程优先级
mythread1.setPriority(10);
//获取线程的优先级
System.out.println(mythread1.getPriority());
System.out.println(mythread2.getPriority());
mythread1.start();
mythread2.start();
}
}
2、join()
等待这个线程死亡,才可以继续下一个线程
线程类:
package Thread;
public class Mythread extends Thread{
public Mythread() {
}
public Mythread(String name) {
//supper将参数指向父类的参数构造方法
super(name);
}
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(getName()+">>>>>>"+i);
}
}
}
测试类:
package Thread;
public class Mythreaddemo1 {
public static void main(String[] args) {
Mythread mythread1 = new Mythread("高铁");
Mythread mythread2 = new Mythread("飞机");
Mythread mythread3 = new Mythread("汽车");
mythread1.setPriority(10);
//获取线程的优先级
System.out.println(mythread1.getPriority());
System.out.println(mythread2.getPriority());
System.out.println(mythread3.getPriority());
mythread1.start();
//等待线程mythread1(高铁)执行完成,另外2个线程才可以继续执行
try {
mythread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
mythread2.start();
mythread3.start();
}
}
3、setDaemon()
设置一个主线程,将其他线程设置为守护线程,当主线程执行完成之后,守护线程随即也自动结束
线程类:
package Thread;
public class Mythread extends Thread{
public Mythread() {
}
public Mythread(String name) {
//supper将参数指向父类的参数构造方法
super(name);
}
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(getName()+">>>>>>"+i);
}
}
}
测试类:
package Thread;
public class Mythreaddemo1 {
public static void main(String[] args) {
Mythread mythread1 = new Mythread("高铁");
Mythread mythread2 = new Mythread("飞机");
Mythread mythread3 = new Mythread("汽车");
mythread1.setPriority(10);
//获取线程的优先级
System.out.println(mythread1.getPriority());
System.out.println(mythread2.getPriority());
System.out.println(mythread3.getPriority());
//设置主线程
Thread.currentThread().setName("小毛驴车");
//mythread1,mythread2,mythread3设置为守护线程,当主线程(小毛驴车)结束后,守护线程随即也结束
mythread1.setDaemon(true);
mythread2.setDaemon(true);
mythread3.setDaemon(true);
mythread1.start();
mythread2.start();
mythread3.start();
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+":"+i);
}
}
}
方式二:实现Runnable类
- 定义一个类MyRunnable实现Runnable接口
- 在MyRunnable类中重写run()方法
- 创建MyRunnable类的对象
- 创建Thread类的对象,把MyRunnable对象作为构造方法的参数
- 启动线程
实现Runnable接口并重写run方法:
package Thread;
public class MyRunnable implements Runnable{
public MyRunnable() {
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+">>>>"+i);
}
}
}
测试类:
package Thread;
public class MyRunnableDemo1 {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
Thread thread1 = new Thread(runnable,"飞机");
Thread thread2 = new Thread(runnable,"高铁");
thread1.start();
thread2.start();
}
}