Java 多线程

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
    }
  }
}

  

代理模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
  }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
    }
  }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
    }
  }
}

  

多线程的问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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--);
    }
  }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

1
2
3
4
5
6
7
8
package ersatz.thread;
 
public class T {
  public static void main(String[] args) {
    Runtime runtime = Runtime.getRuntime();
    System.out.println(runtime.availableProcessors());
  }
}

  

posted @   ascertain  阅读(23)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-08-01 Jenkins卡在初始界面
2020-08-01 SQL server 库一直处于还原状态
点击右上角即可分享
微信分享提示