JAVA_基础多线程(一)

多线程

多线程的创建方式一
一、创建一个继承于Thread类的子类
二、重写Thread类的run() 将此线程执行的操作声明在run()中
三、创建Thread类的子类的对象
4. 通过此对象调用start()

  • 启动当前线程
  • 调用当前线程的run()
    问题一:我们不能直接调用run()的方式启动线程。(直接调用,则是在mian()主线程运行的)
public class ThreadDemo {
    public static void main(String[] args) {
        //③创建Thread类的子类的对象
        MyThread1 m1 = new MyThread1();
        MyThread2 m2 = new MyThread2();
        //方法一:④通过此对象调用start()
        m1.start();
        m2.start();
        //方法二:④创建Thread类的匿名子类的方式
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {a
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 != 0 ) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
    }
}
//①创建一个继承于Thread类的子类
class MyThread1 extends Thread {
    //②重写Thread类的run() 将此线程执行的操作声明在run()中
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0 ) {
                System.out.println(Thread.currentThread().getName() + ":" +i);
            }
        }
    }
}
//①创建一个继承于Thread类的子类
class MyThread2 extends Thread {
    //②重写Thread类的run() 将此线程执行的操作声明在run()中
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 != 0 ) {
                System.out.println(Thread.currentThread().getName() + ":" + i);
            }
        }
    }
}

测试Thread中的常用方法

start():启动当前线程,调用当前线程的run()方法。
run():通常需要重写Thread类中的此方法,将创建的线程要执行的操作声明在此方法中。
currentThread():静态方法,返回当前代码的线程。
getName():获取当前线程的名字。
setName():设置当前线程的名字。
yield():释放当前CPU的执行。
join():在线程A中调用线程B的join(),此时线程A就进入阻塞状态,直到线程B完全执行完之后,线程A才结束阻塞状态。
stop():当执行此方法时,强制结束当前线程。(已过时)
sleep():让当前线程“睡眠”指定的millitime毫秒,在指定的millitime毫秒时间内,当前线程是阻塞状态。
isAlive():判断当前线程是否存活。

线程的优先级

NORM_PRIORITY = 5;(默认线程级)
MIN_PRIORITY = 1;
MAX_PRIORITY = 10;

如何获取和设置当前线程的优先级
getPriority():获取当前线程的优先级。
setPriority(int p):设置当前线程的优先级。

高优先级的线程要抢占低优先级线程CPU的执行权,但是只是从概率上讲,高优先级的线程高概率的情况下被执行。并不意味着只有当高优先级的线程执行完以后,才去执行低优先级。

创建多线程方式二(实现Runnable接口)

① 创建一个实现了Runnable接口的类。
② 实现类去实现Runnable中的抽象方法:run()
③ 创建实现类的对象。
④ 将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象。
⑤ 通过Thread类的对象调用start()
第一步:启动线程。
第二步:调用当前线程的run()→调用了Runnable类型的targetrun()

//① 创建一个实现了`Runnable`接口的类
class RThread implements Runnable {

    //② 实现类去实现`Runnable`中的抽象方法:`run()`
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
}
public class ThreadTest1 {
    public static void main(String[] args) {
        //③ 创建实现类的对象
        RThread rThread = new RThread();

        //④ 将此对象作为参数传递到Thread类的构造器中,创建Thread类的对象
        Thread thread1 = new Thread(rThread);
        thread1.setName("线程一");
        //⑤ 通过Thread类的对象调用start()
        thread1.start();

        //再启动一个线程,遍历100以内的偶数
        Thread thread2 = new Thread(rThread);
        thread2.setName("线程二");
        thread2.start();
    }
}

比较创建线程的两种方式

开发中:优先选择,实现Runnable接口的方式。
原因:
1.继承的方式有单继承的局限性,而实现的方法没有类的单继承的局限性。
2.实现的方法更适合来处理多个线程有共享数据的情况。

相同点:两中方式都需要重写run(),将线程要执行的逻辑声明在run()中。
联系public class Thread implements Runnable{}Thread也是实现Runnable接口,也重写了run()方法。

Tags: