天天看点

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;
    }
    
  }
}      

继续阅读