Java 多线程

  

package ersatz.thread;

public class T1 {
  public static void main(String[] args) throws InterruptedException {
    C c = new C();
    c.start();
    for (int i = 0; i < 60; ++i) {
      System.out.println(Thread.currentThread().getName()+' '+i);
      Thread.sleep(1000);
    }
  }
}

class C extends Thread {
  private int c;

  @Override
  public void run() {
    while (true) {

      System.out.println("vbn " + c++ + ' ' + Thread.currentThread().getName());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (c == 80) break;
    }
  }
}

  

代理模式

package ersatz.thread;

public class T2 {
  public static void main(String[] args) {
    Pig pig = new Pig();
    ThreadProxy threadProxy = new ThreadProxy(pig);
    threadProxy.start();
  }
}

class Animal {
}

class Pig extends Animal implements Runnable {
  @Override
  public void run() {
    System.out.println("B run()");
  }
}

class ThreadProxy implements Runnable {
  private Runnable target = null;

  @Override
  public void run() {
    if (target != null) {
      target.run();
    }
  }

  public ThreadProxy(Runnable target) {
    this.target = target;
  }

  public void start() {
    start0();
  }

  public void start0() {
    run();
  }
}

  

package ersatz.thread;

public class T1 {
  public static void main(String[] args) {
    B b = new B();
    Thread thread = new Thread(b);
    thread.start();
  }
}

class B implements Runnable {
  int c;

  @Override
  public void run() {
    while (true) {
      System.out.println(this.getClass().getName() + ' ' + ++c + ' ' + Thread.currentThread().getName());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (c == 5) break;
    }
  }
}

  

package ersatz.thread;

public class T {
  public static void main(String[] args) {
    T1 t1 = new T1();
    T2 t2 = new T2();
    Thread thread = new Thread(t1);
    Thread thread1 = new Thread(t2);
    thread1.start();
    thread.start();
  }
}

class T1 implements Runnable {
  int c;

  @Override
  public void run() {
    while (true) {
      System.out.println(this.getClass().getName() + ' ' + ++c);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (c == 50) break;
    }
  }
}

class T2 implements Runnable {
  int c;

  @Override
  public void run() {
    while (true) {
      System.out.println(this.getClass().getName() + ' ' + ++c);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (c == 60) break;
    }
  }
}

  

多线程的问题

package ersatz.thread;

public class T {
  public static void main(String[] args) {
    Ticket thread = new Ticket();
    Ticket thread1 = new Ticket();
    Ticket thread2 = new Ticket();
    Ticket thread3 = new Ticket();
    Ticket thread4 = new Ticket();
    thread.start();
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
  }
}

class Ticket extends Thread {
  private static int n = 60;

  @Override
  public void run() {
    while (true) {
      if (n <= 0) {
        System.out.println("tickets out of number");
        break;
      }
      try {
        Thread.sleep(80);
      } catch (Exception e) {
        e.printStackTrace();
      }
      System.out.println(Thread.currentThread().getName() + " sell " + "n = " + n--);
    }
  }
}

  

package ersatz.thread;

public class T {
  public static void main(String[] args) {
    Ticket ticket = new Ticket();
    new Thread(ticket).start();
    new Thread(ticket).start();
    new Thread(ticket).start();
    new Thread(ticket).start();
    new Thread(ticket).start();
  }
}

class Ticket implements Runnable {
  private int n = 60;

  @Override
  public void run() {
    while (true) {
      if (n <= 0) {
        System.out.println("tickets out of number");
        break;
      }
      try {
        Thread.sleep(80);
      } catch (Exception e) {
        e.printStackTrace();
      }
      System.out.println(Thread.currentThread().getName() + " sell " + "n = " + n--);
    }
  }
}

  

查看可用cpu

package ersatz.thread;

public class T {
  public static void main(String[] args) {
    Runtime runtime = Runtime.getRuntime();
    System.out.println(runtime.availableProcessors());
  }
}

  

posted @ 2021-08-01 15:03  ascertain  阅读(22)  评论(0编辑  收藏  举报