欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java多线程之Thread VS Runnable

程序员文章站 2022-05-05 22:45:28
...

0.线程的生命周期

java多线程之Thread VS Runnable

1.java实现多线程方式一:

java多线程之Thread VS Runnable

2.java实现多线程方式二:

java多线程之Thread VS Runnable

3.两种方式的比较:

package ThreadCompare;

public class MyThreadByRunnable implements Runnable {

    private int ticket = 5;

    @Override
    public void run() {
        while(ticket>0) {
            ticket--;
            System.out.println(
                    MyThreadByThread.currentThread().getName()+"卖了1张票,还剩"+ticket+"张");

        }
    }
}
package ThreadCompare;

public class MyThreadByThread extends Thread {
    private int ticket = 5;

    @Override
    public void run() {
        while(ticket>0) {
            ticket--;
            System.out.println(
                    MyThreadByThread.currentThread().getName()+"卖了1张票,还剩"+ticket+"张");

        }
    }
}
package ThreadCompare;

import java.util.Scanner;

public class MainTest {

    public void testThread() {
        MyThreadByThread mt1 = new MyThreadByThread();
        MyThreadByThread mt2 = new MyThreadByThread();
        MyThreadByThread mt3 = new MyThreadByThread();

        mt1.start();
        mt2.start();
        mt3.start();
    }

    public void testRunnable() {
        MyThreadByRunnable mt = new MyThreadByRunnable();
        Thread t1 = new Thread(mt,"窗口1");
        Thread t2 = new Thread(mt,"窗口2");
        Thread t3 = new Thread(mt,"窗口3");

        t1.start();
        t2.start();
        t3.start();
    }


    public void demonThreadTest() {

        System.out.println("*********主线程开始执行了*********");


        DemonThread dt = new DemonThread();
        Thread t = new Thread(dt,"demon");
        t.setDaemon(true);
        t.start();

        Scanner sc = new Scanner(System.in);
        sc.next();

        System.out.println("*********主线程执行结束了*********");

    }

    public static void main(String[] args) {
        //new MainTest().testThread();
        //new MainTest().testRunnable();
        new MainTest().demonThreadTest();
    }
}

执行new MainTest().testRunnable()结果
java多线程之Thread VS Runnable

执行new MainTest().testThread()结果
java多线程之Thread VS Runnable

所以:

Runnable方式可以避免java单继承带来的缺陷,想实现就直接实现接口即可
Runnable实现的线程都是可以由该接口来进行连接,适用于多个线程处理同一个Runnable对象的情况

4.守护线程

Runnable实现方式设置守护线程
通过setDemon(true)方法实现(代码如上所示)。

package ThreadCompare;

public class DemonThread implements Runnable {

    private int count = 0;

    @Override
    public void run() {

        System.out.println("*********守护线程开始执行了*********");
        while (count<100) {
            count++;
            System.out.println("线程执行了 "+count+" 次");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("*********守护线程执行结束了*********");
    }
}

守护线程:
java多线程之Thread VS Runnable

5.线程同步—synchronized用法

java多线程之Thread VS Runnable

分析:

给方法加synchronized 会使得整个方法都是在锁保护(或者叫同步控制)之内,当方法较为冗长时开销较大
只给特定代码块添加synchronized效率高