天天看點

java學習(22)線程(1)

package com.shuiyixin1;

public class learnJ_013xiancheng {

  /**
   * @作者:shuiyixin
   * @日期:2018.02.26
   * @内容:線程
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    //1.建立一個Cat013對象
    //如果采用Cat013 cat013 = new Cat013();會報錯:
    //No enclosing instance of type learnJ_013xiancheng is accessible. Must qualify the allocation with an enclosing instance of type learnJ_013xiancheng (e.g. x.new A() where x is an instance of learnJ_013xiancheng).
    //意思是:沒有任何類型 TestThread 的外層執行個體可通路。必須用類型 TestThread 的外層執行個體(例如,x.new A(),其中 x 是 TestThread 的執行個體)來限定配置設定。
    //解決方法1:将外部類實作對象,并調用方法。
    learnJ_013xiancheng obj = new learnJ_013xiancheng();
    Cat013 cat013 = obj.new Cat013();
    cat013.start();
    //解決方法2:将内部類設定為靜态類,因為類中的靜态方法不能直接調用動态方法。
    //隻有将某個内部類修飾為靜态類,然後才能夠在靜态類中調用該類的成員變量與成員方法。
    Dog013 dog013 = new Dog013();
    
    Fish013 fish = obj.new Fish013();
    //建立一個Thread
    Thread t = new Thread(fish);
    t.start();
    
    /**
     * 由于雙方都使用了sleep,代碼輸出如下:
     * hello world!1
     * hello fish!1
     * hello world!2
     * hello fish!2
     * hello world!3
     * hello fish!3
     * 如果有一方沒有使用sleep,即沒有阻塞狀态,隻要運作到,就會全部優先輸出。
     * 是否是交替執行,取決于sleep的數值。
     * 注意,線程一旦定好,程式員無法控制。
     */
  }

  class Cat013 extends Thread{
    int times = 0;
    public void run(){
      while (times<3) {
        try {
          //sleep會讓線程進入到Blocked狀态并釋放資源。
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        System.out.println("hello world!" + (times+1));
        times++;
        
      }
    }
  }
  
  public static class Dog013 extends Thread{
    public void run(){
      //System.out.println("hello world!");
    }
  }
  public class Fish013 implements Runnable{

    int times = 0;
    public void run() {
      while (times<3) {
        try {
          //sleep會讓線程進入到Blocked狀态并釋放資源。
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        System.out.println("hello fish!" + (times+1));
        times++;
      }
    }
   }
  public class Bird013 implements Runnable{
    int n = 0;
    int res = 0;
    int times = 0;
    
    public void run() {
      while (times == n ) {
        try {
          //sleep會讓線程進入到Blocked狀态并釋放資源。
          Thread.sleep(1000);
        } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        res += ++times;
      }
      System.out.println("目前結果是:" + res);
    }
    public int getN() {
      return n;
    }
    public void setN(int n) {
      this.n = n;
    }
    public int getRes() {
      return res;
    }
    public void setRes(int res) {
      this.res = res;
    }
    public int getTimes() {
      return times;
    }
    public void setTimes(int times) {
      this.times = times;
    }
    
  }
}      

繼續閱讀