java多线程

多线程

线程的创建方式:

1.继承Thread类重写run()

public class ThreadRunn extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("run重写");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new ThreadRunn();
        t1.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main");
        }
    }
}

2.实现Runnable接口重写run()

public class Runnableee implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("run重写");
        }
    }

    public static void main(String[] args) {
        Runnableee r = new Runnableee();
        Thread t1 = new Thread(r);
        t1.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("main");
        }
    }
}

3.匿名内部类创建线程

public class NoNameee {
    public static void main(String[] args) {
        new Thread() {
            @Override
            public void run() {
                System.out.println("...");
            }
        }.start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("。。。");
            }
        }).start();
    }
}

线程id与name


public class ThreadNameee extends Thread {

    //修改默认线程名,需先设置构造方法
    public ThreadNameee(String str) {
        super(str);
    }

    @Override
    public void run() {
        System.out.println(getId() + " " + getName());
        setName("xiao");
        System.out.println(getId() + " " + getName());
    }

    public static void main(String[] args) {
        ThreadNameee t1 = new ThreadNameee("ying");
        t1.start();

        //获取主线程的引用
        Thread t2 = Thread.currentThread();
        System.out.println("主线程的id:" + t2.getId());
    }
}

线程睡眠与停止


public class ThreadTimee extends Thread {

    private static boolean flag = true;

    @Override
    public void run() {
        while (flag) {
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            System.out.println(sdf.format(date));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new ThreadTimee();
        t1.start();

        //主线程睡眠5秒后停止
        System.out.println("start sleep");
        Thread.sleep(5000);
        flag = false;
        //t1.stop();
        System.out.println("stop thread");
    }
}

修改线程优先级,优先级越高的不一定先执行

public class ThreadPriorityy extends Thread {

    @Override
    public void run() {
        System.out.println("run:" + getPriority());
    }

    public static void main(String[] args) {
        Thread thread = new ThreadPriorityy();
        thread.start();
        thread.setPriority(MAX_PRIORITY);
        System.out.println("main:" + Thread.currentThread().getPriority());
    }
}

join实例


public class ThreadJoin extends Thread {

    @Override
    public void run() {
        System.out.println("start");
        for (int i = 0; i < 5; i++) {
            System.out.println(i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("end");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new ThreadJoin();
        thread.start();

        System.out.println("开始等待");
        //thread.join();
        thread.join(3000);//等待3秒
        System.out.println("结束等待");
    }
}

守护线程

//守护线程会随主线程结束,不论是否执行完全
public class ThreadDaemonn extends Thread {

    @Override
    public void run() {
        System.out.println(isDaemon() ? "是守护线程" : "不是守护线程");
        for (int i = 0; i < 20; i++) {
            System.out.println("子线程:" + i);
        }
    }

    public static void main(String[] args) {
        ThreadDaemonn thread = new ThreadDaemonn();
        thread.setDaemon(true);
        thread.start();
        for (int i = 0; i < 20; i++) {
            System.out.println("主线程");
        }
    }
}

线程同步

public synchronized void run() {
        System.out.println("线程启动...");
        //synchronized (this) {
            
            ...
            
        //}
    }

死锁

public void run(){
    synchronized(a){
       synchronized(b){
            ...
        } 
    }
}

public void run(){
    synchronized(b){
       synchronized(a){
            ...
        } 
    }
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦