zoukankan      html  css  js  c++  java
  • Java多线程学习关于Thread类

    Java多线程学习

    • 继承Thread类

      • 重写run方法

        官方api里截取的代码

        https://docs.oracle.com/javase/9/docs/api/java/lang/Thread.html
        
        class PrimeThread extends Thread {
                 long minPrime;
                 PrimeThread(long minPrime) {
                     this.minPrime = minPrime;
                 }
        
                 public void run() {
                     // compute primes larger than minPrime
                      . . .
                 }
             }
        

        主方法调用

        PrimeThread p = new PrimeThread(143);
             p.start();
        

        第二种方法

        class PrimeRun implements Runnable {
                 long minPrime;
                 PrimeRun(long minPrime) {
                     this.minPrime = minPrime;
                 }
        
                 public void run() {
                     // compute primes larger than minPrime
                      . . .
                 }
             }
         
        

        主方法调用

        PrimeRun p = new PrimeRun(143);
             new Thread(p).start();
        

      可以看到 官方推荐使用 start();

      可为什么 要使用 start方法?

      public synchronized void start() {
            
              if (threadStatus != 0)
                  throw new IllegalThreadStateException();
              group.add(this);
              boolean started = false;
              try {
                  start0();
                  started = true;
              } finally {
                  try {
                      if (!started) {
                          group.threadStartFailed(this);
                      }
                  } catch (Throwable ignore) {
                  }
              }
          }
      
          private native void start0();
      

      这里 可以看到源码 里 会抛出一个异常

      IllegalThreadStateException()

      这个异常是 通过 throw 写在方法上的 自己感觉 8成是RuntimeException

      点开 看看源码

      IllegalThreadStateException extends IllegalArgumentException
      
      class IllegalArgumentException extends RuntimeException
      

      本人天才...

      说起异常

      异常 有一个大类 Exception 所有的异常基本都继承这个大类里 这个大类 可以简要分成 两种

      一种是程序员可处理的异常 就是RuntimeException 运行时异常 举例来说就是 数组越界等

      还有一个是程序员 处理不了的异常 IOException 这种异常啊 基本就是 打开一个不存在的文件

      image-20210309212727651

      要捕获异常的话

      使用 printStackTrace方法非常的方便

      言归正传在start方法中 IllegalThreadStateException() 这个异常 是个什么呢?

      public void start​()
      Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
      The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
      
      It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
      
      Throws:
      IllegalThreadStateException - if the thread was already started.
      See Also:
      run(), stop()
      

      看官方文档吧

      IllegalThreadStateException - if the thread was already started.

      如果线程已经开启 哪么 抛出这个异常

      试试吧? 重复进行了 线程的启动 m.start();

      class Mythread extends Thread {
          private String title;
          public Mythread (String title) {
              this.title = title;
          }
          @Override
          public void run() {
              for (int i = 0 ; i < 10; i++) {
                  System.out.println(this.title+"线程:"+i);
              }
          }
      }
      
      public class duoxianc {
          public static void main(String args[]) {
              Mythread m = new Mythread("A");
              m.start();
              m.start();
              new Mythread("b").start();
              new Mythread("c").start();
              new Mythread("d").start();
      
          }
      }
      
      

      结果打印如下 别问我为什么 不是分这来的 电脑太快了

      Exception in thread "main" java.lang.IllegalThreadStateException
      	at java.lang.Thread.start(Thread.java:708)
      	at duoxianc.main(duoxianc.java:18)
      A线程:0
      A线程:1
      A线程:2
      A线程:3
      A线程:4
      A线程:5
      A线程:6
      A线程:7
      A线程:8
      A线程:9
      

      Thread类 提供了 一个start0方法

      private native void start0();

      什么是native呢?我也不知道 但是我会百度

      native是一个计算机函数,一个Native Method就是一个Java调用非Java代码的接口。方法的实现由非Java语言实现,比如C或C++。
      

      说点人话吧 我感觉就是 在不同的操作系统上 实现 start() 方法

    Therad类 就说这么多吧

    总结一下 两点至关重要的 重写run方法调用start方法

  • 相关阅读:
    卢卡斯定理算法模板
    求组合数的O(n^2)和O(n)解法及模板
    求逆元的方法及模板
    扩展欧基里德算法模板
    牛客练习赛43-F(简单容斥)
    容斥原理
    牛客网练习赛43-C(图论)
    折半搜索
    枚举+树状数组(经典)
    思维并查集/网络流和二分
  • 原文地址:https://www.cnblogs.com/laowt/p/14508342.html
Copyright © 2011-2022 走看看