Java入门12(多线程)
多线程线程的实现方式
[*]继承 Thread 类:一旦继承了 Thread 类,就不能再继承其他类了,可拓展性差
[*]实现 Runnable 接口:仍然可以继承其他类,可拓展性较好
[*]使用线程池
继承Thread 类
不能通过线程对象调用 run() 方法,需要通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中
// 这是一个简单的栗子
public class StudentThread extends Thread{
public StudentThread(String name) {
super(name);
}
@Override
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println("This is a test thread!");
System.out.println(this.getName());
}
}
}
// 启动类
public static void main(String[] args) {
Thread t1 = new StudentThread();
// 不能通过线程对象调用run()方法
// 通过 t1.start() 方法,使线程进入到就绪状态,只要进入到就绪状态的线程才有机会被JVM调度选中
t1.start();
}实现 Runable 接口
实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化
// 介还是一个简单的栗子
public class StudentThreadRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println("This is a test thread!");
System.out.println(Thread.currentThread().getName());
}
}
}
// 启动类
public static void main(String[] args) {
// 实现方式需要借助 Thread 类的构造函数,才能完成线程对象的实例化
StudentThreadRunnable studentThreadRunnable = new StudentThreadRunnable();
Thread t01 = new Thread(studentThreadRunnable);
t01.setName("robot010");
t01.start();
}匿名内部类实现
在类中直接书写一个当前类的子类,这个类默认不需要提供名称,类名由JVM临时分配
public static void main(String[] args) {
Thread t01 = new Thread(){
@Override
public void run() {
for (int i = 0; i < 2; i++) {
System.out.println("This is a test thread!");
}
System.out.println(Thread.currentThread().getName()); // 线程名
System.out.println(this.getClass().getName()); // 匿名线程类类名
}
};
t01.start();
}线程的休眠(sleep方法)
sleep方法,会使当前线程暂停运行指定时间,单位为毫秒(ms),其他线程可以在sleep时间内,获取JVM的调度资源
// 这是一个计时器
public class TimeCount implements Runnable{
@Override
public void run() {
int count = 0;
while(true){
System.out.println(count);
count++;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
// 测试类
public static void main(String[] args) {
System.out.println("这是main方法运行的时候,开启的主线程~~~");
TimeCount timeCount = new TimeCount();
Thread timeThread = new Thread(timeCount);
System.out.println("开启计时器");
timeThread.start();
System.out.println("主线程即将休眠>>>>>>>>>>>");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(">>>>>>>>>>>主线程休眠结束~~~~~");
}线程的加入(join方法)
被 join 的线程会等待 join 的线程运行结束之后,才能继续运行自己的代码
public static void main(String[] args) {
Thread thread01 = new Thread(){
@Override
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("This is thread-01!");
}
}
};
thread01.start();
try {
thread01.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread thread02 = new Thread(){
@Override
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("This is thread-02!");
}
}
};
thread02.start();
}
// thread02 会等待 thread01 完全跑完,才会开始自己的线程线程的优先级(priority方法)
优先级高的线程会有更大的几率竞争到JVM的调度资源,但是高优先级并不代表绝对,充满玄学✨
public static void main(String[] args) {
Thread thread01 = new Thread(){
@Override
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("This is thread-01! " + Thread.currentThread().getPriority());
}
}
};
Thread thread02 = new Thread(){
@Override
public void run(){
for (int i = 0; i < 10; i++) {
System.out.println("This is thread-02! " + Thread.currentThread().getPriority());
}
}
};
thread01.setPriority(1);
thread02.setPriority(10);
// 尽管thread02优先级高于thread01,但是也有可能
thread01.start();
thread02.start();
}线程的让步(yield方法)
立刻让出JVM的调度资源,并且重新参与到竞争中
public static void main(String[] args) { Thread thread01 = new Thread(){ @Override public void run(){ for (int i = 1; i
页:
[1]