ToB企服应用市场:ToB评测及商务社交产业平台

标题: 多线程 [打印本页]

作者: 瑞星    时间: 2023-4-4 14:04
标题: 多线程
多线程

多线程概述

并发和并行

并行:在同一时间,多个任务分别在多个CPU上进行。
并发:在同一时间,多个任务在同一个CPU交替进行。
线程和进程

进程
线程不能单独存在,线程是进程的一部分,一个进程可以有多可线程,但必须至少有一个线程。
多线程实现

自定义类通过继承Thread类,再重写run() 方法
  1. public class ThreadDemo extends Thread{
  2.     @Override
  3.     public void run() {
  4.         for (int i = 0; i < 100; i++) {
  5.             System.out.println(getName()+"的第"+i+"次");
  6.         }
  7.     }
  8. }
复制代码
在测试类中创建自定义类对象
  1. public class TestDemo {
  2.     public static void main(String[] args) {
  3.         ThreadDemo t1 = new ThreadDemo();
  4.         //为线程设置名字
  5.         t1.setName("线程1");
  6.         //start() 方法告诉JVM程序会启用多线程,并自动调用run() 方法
  7.         //直接执行run() 方法,那么就是执行一个普通的单线程方法
  8.         t1.start();
  9.         ThreadDemo t2 = new ThreadDemo();
  10.         t2.setName("线程2");
  11.         t2.start();
  12.     }
  13. }
复制代码
  1. public class RunnableDemo implements Runnable{
  2.     @Override
  3.     public void run() {
  4.         for (int i = 0; i < 100; i++) {
  5.             //让线程停止指定毫秒数
  6.             //父类Thread没有抛出异常,所以子类只能自己捕获异常
  7.             try {
  8.                 Thread.sleep(1);
  9.             } catch (InterruptedException e) {
  10.                 throw new RuntimeException(e);
  11.             }
  12.             //调用Thread的静态方法currentThread(),可以直接获取当前的进程
  13.             System.out.println("这是线程"+Thread.currentThread().getName()+i);
  14.         }
  15.     }
  16. }
复制代码
测试类
  1. public class TestDemo2 {
  2.     public static void main(String[] args) {
  3.         RunnableDemo ra = new RunnableDemo();
  4.         //调用Thread() 的带参方法,传入一个Runnable的实现类对象
  5.         Thread thread = new Thread(ra);
  6.         thread.setName("西安");
  7.         //为该线程设置优先级1(min)-10(max),优先级越高,抢占CPU的几率就越大,但不会达到100%
  8.         thread.setPriority(10);
  9.         //获取优先级
  10.         System.out.println(thread.getPriority());
  11.         thread.start();
  12.         
  13.         Thread thread2 = new Thread(ra);
  14.         thread2.setPriority(1);
  15.         //获取优先级
  16.         System.out.println(thread2.getPriority());
  17.         thread2.setName("杭州");
  18.         thread2.start();
  19.     }
  20. }
复制代码
  1. public class CallableDemo implements Callable<String> {
  2.     @Override
  3.     public String call() throws Exception {
  4.         for (int i = 0; i < 100; i++) {
  5.             System.out.println("和班主任请假第"+i+"次");
  6.         }
  7.         return "同意";
  8.     }
  9. }
复制代码
测试类
  1. public class TestDemo {
  2.     public static void main(String[] args) throws ExecutionException, InterruptedException {
  3.         CallableDemo cd = new CallableDemo();
  4.         FutureTask<String> ft = new FutureTask<>(cd);
  5.         Thread thread = new Thread(ft);
  6.         thread.start();
  7.         FutureTask<String> ft2 = new FutureTask<>(cd);
  8.         Thread thread2 = new Thread(ft2);
  9.         thread2.start();
  10.         //注意如果在start方法之前 调用get() 方法,意味着在开启线程之前获取返回值,但是此时线程尚未开启,这就照成死锁
  11.         String o = ft.get();
  12.         String o2 = ft2.get();
  13.         System.out.println(o);
  14.         System.out.println(o2);
  15.     }
  16. }
复制代码
总结如下:

优点:子类可以直接调用父类的方法,缺点,对子类限制比较大,不能再继承其他类
优点:通过实现接口的方式,子类还可以继承其他类,拓展性更强
缺点:不能直接启用多线程,需要先创建Runnable实现类对象,再使用Thread的带参构造传入Runnable实现类对象,再通过Thread对象启用多线程
优点:通过实现接口的方式,子类还可以继承其他类,执行线程后还可以返回值
缺点:不能直接启用多线程,需要先需要先创建Callable实现类对象,再使用FutureTask的带参构造传入Runnable实现类对象,再使用Thread的带参构造传入FutureTask类对象,再通过Thread对象启用多线程
守护线程

守护线程会因为普通线程结束而结束,无论守护线程是否执行完毕
代码如下:
  1. public class DaemonDemo {
  2.     public static void main(String[] args) {
  3.         Test1 t1 = new Test1();
  4.         t1.setName("主人");
  5.         t1.setPriority(1);
  6.         t1.start();
  7.         
  8.         Test2 t2 = new Test2();
  9.         t2.setDaemon(true);
  10.         t2.setName("保镖");
  11.         t2.setPriority(10);
  12.         t2.start();
  13.     }
  14. }
  15. /**
  16. * 主人线程
  17. */
  18. public class Test1 extends Thread{
  19.     @Override
  20.     public void run() {
  21.         for (int i = 0; i < 10; i++) {
  22.             System.out.println(getName()+"..."+i);
  23.         }
  24.     }
  25. }
  26. /**
  27. * 保镖线程
  28. */
  29. public class Test2 extends Thread{
  30.     @Override
  31.     public void run() {
  32.         try {
  33.             Thread.sleep(3);
  34.         } catch (InterruptedException e) {
  35.             throw new RuntimeException(e);
  36.         }
  37.         for (int i = 0; i < 100; i++) {
  38.             System.out.println(getName()+"==="+i);
  39.         }
  40.     }
  41. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4