Thread基本用法总结

1.线程创建

方法一:继承Thread类,重写run方法

class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("hello thread");
    }
}

public class ThreadDemo8 {
    public static void main(String[] args) {
        Thread t = new MyThread();
        t.start();
    }
}

方法二:实现Runnable类,重写run方法

class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("hello thread");
    }
}

public class ThreadDemo8 {
    public static void main(String[] args) {
        Runnable runnable = new MyRunnable();
        Thread t = new Thread(runnable);
        t.start();
    }
}

方法三:和方法一一样,不过用内部类实现

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

方法四:和方法二一样,也是内部类实现

public class ThreadDemo8 {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("hello thread");
            }
        });
        t.start();
    }
}

方法五:lambda方法实现

public class ThreadDemo8 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            System.out.println("hello thread");
        });
        t.start();
    }
}

2.线程中断

方法一:使用标志位来控制线程是否要停止

public class ThreadDemo8 {
    public static boolean flag = true;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (flag) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
        Thread.sleep(5000);
        flag = false;
    }
}

方法二:使用Thread自带的标志位来进行判定

public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        t.start();
        Thread.sleep(5000);
        t.interrupt();
    }
}

3.线程等待

public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("线程t在执行");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程t执行完");
        });
        t.start();
        //主线程等待t线程执行完
        t.join();
        System.out.println("主线程执行完");
    }
}

4.线程休眠

public class ThreadDemo8 {
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread.sleep(3 * 1000);
        long end = System.currentTimeMillis();
        System.out.println("主线程休眠了" + ((end - start) / 1000) + "秒");
    }
}

5.线程实例

public class ThreadDemo8 {

    public static void main(String[] args) throws InterruptedException {
        serial();
        concurrency();
    }

    public static void concurrency() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = 0;
                for (long i = 0; i < 10_0000_0000; i++) {
                    a--;
                }
            }
        });
        thread.start();
        int b = 0;
        for (long i = 0; i < 10_0000_0000; i++) {
            b--;
        }
        thread.join();
        long end = System.currentTimeMillis();
        double ms = (end - begin) * 1.0;
        System.out.printf("并发:%f 毫秒%n", ms);
    }

    private static void serial() {
        long begin = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < 10_0000_0000; i++) {
            a--;
        }
        int b = 0;
        for (long i = 0; i < 10_0000_0000; i++) {
            b--;
        }
        long end = System.currentTimeMillis();
        double ms = (end - begin) * 1.0;
        System.out.printf("串行:%f 毫秒%n", ms);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值