线程及开启方式

系统管理员 2022-02-01 08:15 303阅读 0赞

文章目录

  • 线程及开启方式
      • 线程
      • 方式一(继承Thread类)
      • 方式二(实现Runnable接口)
      • 方式三(实现Callable接口)

线程及开启方式

线程

进程:正在执行的程序
线程:具有完成独立任务的一条执行路径
多线程:一个程序拥有多条线程

多线程的好处:

  1. 可以提高进程和CPU的使用率
  2. 能够让多个程序看起来像同时执行
  3. 防止单线程出现阻塞
  4. 用于处理耗时任务

题外话:关于并发和并行(以后会详写)

  • 并发:在一段时间间隔内处理多个事务的能力;
  • 并行:在某一时刻,同时处理多个事务的能力;

方式一(继承Thread类)

注意:Thread类是一个普通类不是接口;
要想开启某一类的线程时,继承Thread类,并实现run()方法,在主方法中调用start()方法即可;

  1. public static void main(String[] args) {
  2. MyThread my1 = new MyThread("线程1");
  3. MyThread my2 = new MyThread("线程2");
  4. //开启线程1
  5. my1.start();
  6. //开启线程2
  7. my2.start();
  8. }
  9. class MyThread extends Thread{
  10. public MyThread() {
  11. }
  12. public MyThread(String name) {
  13. super(name);
  14. }
  15. // Thread.currentThread()表示当前正在执行的是哪一条线程那么就是对应的那个线程的名称
  16. @Override
  17. public void run() {
  18. for (int i = 0; i < 100; i++) {
  19. System.out.println(Thread.currentThread().getName() + ":" + i);
  20. }
  21. }
  22. }

其结果:线程1和线程2在随机交替打印,原因是因为Cup在执行时是执行的原子性语句,并且随机执行线程。

方式二(实现Runnable接口)

Runnable接口中只有一个抽象方法run();子类实现run()方法即可,不用再继承Thread类。
因为Runnable接口中只有一个抽象方法,即该接口属于函数式接口@FunctionalInterface,可以用Lambda表达式(JDK1.8之后)

  1. public static void main(String[] args) {
  2. MyRunnable mr = new MyRunnable();
  3. //作为参数出入Thread中,以调用start();
  4. Thread t = new Thread(mr);
  5. //Lambda表达式,Thread类也实现了Runnable接口
  6. new Thread(() -> {
  7. for (int i = 0; i < 100; i++) {
  8. System.out.println("Lambda表达式" + i);
  9. }
  10. }).start();
  11. t.start();
  12. }
  13. class MyRunnable implements Runnable {
  14. @Override
  15. public void run() {
  16. for (int i = 0; i < 100; i++) {
  17. System.out.println("Runnable接口" + i);
  18. }
  19. }

方式三(实现Callable接口)

Callable<V>接口与上面两种不同,他是有返回值的;
V call() throws Exception;

一般会和Future接口一起使用,借助FutureTask类来执行,FutureTask同时实现了FutureRunnable接口。

  1. public static void main(String[] args) throws Exception {
  2. Callable<Integer> c = new MyCallable();
  3. FutureTask<Integer> fu =new FutureTask<Integer>(c);
  4. new Thread(fu).start();
  5. //通过FutureTask中的get()方法来获取call()的返回值
  6. System.out.println(fu.get());
  7. //可以通过匿名内部类来实现Callable接口
  8. Callable<Integer> cn = new Callable<Integer>(){
  9. int sum = 0;
  10. @Override
  11. public Integer call() throws Exception {
  12. for(int i = 1;i<100;i++){
  13. sum+=i;
  14. System.out.println(Thread.currentThread().getName()+"实现Callable线程"+sum);
  15. }
  16. return sum;
  17. }
  18. };
  19. }
  20. class MyCallable implements Callable<Integer>{
  21. int sum = 0;
  22. @Override
  23. public Integer call() throws Exception {
  24. for(int i = 1;i<100;i++){
  25. sum+=i;
  26. System.out.println(Thread.currentThread().getName()+"实现Callable线程"+sum);
  27. }
  28. return sum;
  29. }
  30. }

和线程池一起使用:(注意返回值是Future,使用FutureTask接收就要向下转型)

  1. public static void main(String[] args) throws Exception {
  2. ExecutorService pool = Executors.newCachedThreadPool();
  3. Future<Integer> fu = pool.submit(new Callable<Integer>(){
  4. int sum = 0;
  5. @Override
  6. public Integer call() throws Exception {
  7. for(int i = 1;i<100;i++){
  8. sum+=i;
  9. System.out.println(Thread.currentThread().getName()+"实现匿名Callable线程"+sum);
  10. }
  11. return sum;
  12. }
  13. });
  14. new Thread((FutureTask<Integer>)fu).start();
  15. System.out.println(fu.get());

以上
@Fzxey

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读