多线程学习笔记

48 阅读1分钟

多线程学习笔记

1、继承Thread类

public class MyThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("线程运行" + i);
        }
    }
}


public class TestThread {
    public static void main(String[] args) {
        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();

        thread1.start();
        thread2.start();

    }
}

2、实现Runnable接口

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("我实现了Runnable接口 " + i);
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        MyRunnable mt = new MyRunnable();
        Thread t1 = new Thread(mt); //mt只能作为参数传入到Thread中
        t1.start();
    }
}

3、实现callable接口

image-20220105214348352

import java.util.concurrent.Callable;

public class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println("跟女孩表白" + i);
        }
        //返回值就表示线程运行完毕之后的结果
        return "答应";
    }
}


import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable mc = new MyCallable();

        FutureTask<String> ft = new FutureTask<>(mc);

        Thread t1 = new Thread(ft);

        t1.start();

        System.out.println(ft.get());
    }
}

4、三种方式的比较

image-20220105215856434

5、线程的名称

修改线程的名称

1)构造函数Constructor()

2)setName()

public class MyThread extends Thread{
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("我是名字");
        }
    }
}


public class Demo {
    public static void main(String[] args) {
        MyThread t1 = new MyThread("小强");
        MyThread t2 = new MyThread("小美");

//        t1.setName("小强");
//        t2.setName("小美");

        System.out.println(t1);
        System.out.println(t2);
    }
}

6、获取当前线程的名称

/**
 * public static Thread currentThread()
 */
public class Demo {
    public static void main(String[] args) {
        String name = Thread.currentThread().getName();
        System.out.println(name);
    }
}

7、线程休眠

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("睡觉了");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("睡醒了");
    }
}


public class Demo {
    public static void main(String[] args) throws InterruptedException {
//        System.out.println("我刚睡");
//        Thread.sleep(1000);
//        System.out.println("我睡醒了");

        MyRunnable mr = new MyRunnable();
        Thread t1 = new Thread(mr);
        t1.start();
    }
}

8、线程的优先

public int getPriority();
public void setPriority(int num);

Priority最小值为1
Priority最大值为10
import java.util.concurrent.Callable;

public class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "----" + i);
        }
        return "线程执行完毕";
    }
}


import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyCallable mc1 = new MyCallable();
        FutureTask<String> ft1 = new FutureTask<>(mc1);
        Thread t1 = new Thread(ft1);
        t1.setName("小米");
        t1.setPriority(10);
        t1.start();

//        System.out.println(ft.get());

        MyCallable mc2 = new MyCallable();
        FutureTask<String> ft2 = new FutureTask<>(mc2);
        Thread t2 = new Thread(ft2);

        t2.setName("小白");
        t2.setPriority(1);
        t2.start();
    }
}

9、守护线程

public final void setDaemon(boolean on);//设置为守护线程

image-20220105225040234

暂时笔记:黑马JavaEE零基础在线就业班课程_哔哩哔哩_bilibili

明天继续:

//设置为守护线程


[外链图片转存中...(img-NbvKhjSD-1641474940939)]





暂时笔记:[黑马JavaEE零基础在线就业班课程_哔哩哔哩_bilibili](https://www.bilibili.com/video/BV14f4y1x7Qk?p=444)

明天继续:

[外链图片转存中...(img-TpId3WLz-1641474940939)]

[外链图片转存中...(img-2VAJJa3J-1641474940940)]