Thread提供的常见构造器 | 说明 |
---|---|
public Thread(String name) | 可以为当前线程指定名称 |
public Thread(Runnable target) | 封装Runnable对象成为线程对象 |
public Thread(Runnable target,String name) | 封装Runnable对象成为线程对象,并指定线程名称 |
public class Demo01 {
public static void main(String[] args) {
//public Thread(String name); 创建线程对象并设置线程名称
MyThread t1 = new MyThread("火车");
t1.start();
//public Thread(Runnable target); 封装Runnable对象成为线程对象
Thread t2 = new Thread(new MyRunnable(), "高铁");
t2.start();
}
}
//线程实现方式一
class MyThread extends Thread {
public MyThread(){
}
//子类将名称直接交给父类的构造器初始化
public MyThread(String name) {
super(name);
}
@Override
public void run() {
//public void run(); 封装线程任务的方法
for (int i = 1; i <= 10; i++) {
//public String getName(); 获取线程名称
System.out.println(getName() + ":" + i);
}
}
}
//线程实现方式二
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
Thread提供的常用方法 | 说明 |
---|---|
public void run() | 线程的任务方法 |
public void start() | 启动线程 |
public String getName() | 获取当前线程的名称,线程名称默认是Thread-索引 |
public void setName(String name) | 为线程设置名称 |
public static Thread currentThread() | 获取当前线程的执行对象 |
public static void sleep(long time) | 让当前执行的线程休眠多少毫秒后,再继续执行 |
public final void join() | 让调用当前这个方法的线程先执行完 |
public class Demo02 {
public static void main(String[] args) {
MyThread t1 = new MyThread();
//public void setName(); 设置线程名称
t1.setName("火车");
//public void start(); 启动线程
t1.start();
//public Thread(Runnable target); 封装Runnable对象成为线程对象
Thread t2 = new Thread(new MyRunnable(), "高铁");
t2.start();
}
}
//线程实现方式一
class MyThread extends Thread {
@Override
public void run() {
//public void run(); 封装线程任务的方法
for (int i = 1; i <= 10; i++) {
//public String getName(); 获取线程名称
System.out.println(getName() + ":" + i);
}
}
}
//线程实现方式二
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
public static void sleep(long time) | 让当前执行的线程休眠多少毫秒后,再继续执行 |
---|---|
public final void join() | 让调用当前这个方法的线程先执行完 |
public class Demo03 {
public static void main(String[] args) throws InterruptedException {
//public static void sleep(long time); 让当前执行的线程,休眠指定毫秒后继续运行
// System.out.println("测试开始");
// Thread.sleep(3000);
// System.out.println("测试开始");
//public final void join(); 让调用这个方法的线程,启动后优先执行完毕
//Java中如何控制三个线程,按指定顺序执行完毕(三个方法启动后都加join,调整顺序即可实现)
Thread t3 = new Thread(() -> {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "线程3");
t3.start();
t3.join();
Thread t2 = new Thread(() -> {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "线程2");
t2.start();
t2.join();
Thread t1 = new Thread(() -> {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "线程1");
t1.start();
t1.join();
}
}
(1)线程分为两种调度模型
分时调度,所有线程轮流使用CPU,平均分配时间
抢占式调度:优先级高的获取CPU时间相对长一些(不是绝对),如果优先级相同会随机选择,Java中线程的调度模型为抢占式调度,在同一时刻,线程抢夺CPU的执行权是随机的
public final void setDaemon(boolean on):设置当前线程为守护线程,当其他线程执行完毕了,守护线程也就跟着停止了,但不是立刻
public class Demo03 {
public static void main(String[] args) {
//public final void setDaemon(boolean on); 设置当前线程为守护线程
new Thread(() -> {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "线程1").start();
Thread t2 = new Thread(() -> {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "线程2");
//public final void setDaemon(boolean on); 设置当前线程为守护线程
t2.setDaemon(true);
t2.start();
}
}
public final void setPriority():设置线程优先级
public final int getPriority():获取线程优先级
线程优先级高仅是抢到CPU的执行权相对几率大,不是绝对的
public static final int MIN_PRIORITY=1;最低
public static final int NORM_PRIORITY=5;默认
public static final int MAX_PRIORITY=10;最高
public class Demo03 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "飞机");
//public final void setPriority();设置线程优先级
//t1.setPriority(100); //数值超出了范围,抛异常 IllegalArgumentException
t1.setPriority(10);
t1.start();
Thread t2 = new Thread(() -> {
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}, "大炮");
//System.out.println(t2.getPriority()); //默认是5
t2.start();
}
}