阅读 105

线程事件通知 & 线程方法

线程事件通知 & 线程方法

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) throws InterruptedException {
    B b =new B();
    b.start();
    Thread.sleep(5 * 1000);
    b.setLoop(false);
  }
}
 
class B extends Thread {
  private int n;
  private boolean loop =true;
 
  public void setLoop(boolean loop) {
    this.loop = loop;
  }
 
  @Override
  public void run() {
    while (loop) {
      System.out.println("B " + n++);
      try {
        Thread.sleep(1000);
      }catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
 
  }
}

  

interrupt

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
package ersatz.thread;
 
public class T {
  public static void main(String[] args) throws InterruptedException {
    B b =new B();
    b.setName("uiop");
    b.setPriority(Thread.MIN_PRIORITY);
    b.start();
    for (int i = 0; i < 5; ++i) {
      Thread.sleep(1000);
      System.out.println("main thread");
    }
    System.out.println(b.getName() +" b priority " + b.getPriority());
    b.interrupt();
  }
}
 
class B extends Thread {
  @Override
  public void run() {
    while (true) {
      for (int i = 0; i < 100; ++i) {
        System.out.println(Thread.currentThread().getName() +" i = " + i);
      }
      try {
        System.out.println(Thread.currentThread().getName() +" sleeping");
        Thread.sleep(20 * 1000);
      }catch (InterruptedException e) {
        System.out.println(Thread.currentThread().getName() +" interrupted");
      }
    }
  }
}

  

 join

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
package ersatz.thread;
 
public class T {
  public static void main(String[] args) throws InterruptedException {
    B b =new B();
    b.start();
    for (int i = 0; i < 8; ++i) {
      Thread.sleep(1000);
      System.out.println("Main "+i);
      if (i == 3) {
        System.out.println(b.getClass().getName()+" join");
        b.join();
        System.out.println(b.getClass().getName()+" join finish");
      }
    }
  }
}
 
class B extends Thread {
  @Override
  public void run() {
    for (int i = 0; i < 7; ++i) {
      try {
        Thread.sleep(1000);
      }catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println(this.getClass().getName() +' ' + i);
    }
  }
}

  

 

 

Thread.yield() 礼让cpu not comple

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) throws InterruptedException {
    B b =new B();
    b.start();
    for (int i = 0; i < 8; ++i) {
      Thread.sleep(1000);
      System.out.println("Main "+i);
      if (i == 3) {
        System.out.println(b.getClass().getName()+" join");
//        b.join();
        Thread.yield(); // not compel
        System.out.println(b.getClass().getName()+" join finish");
      }
    }
  }
}
 
class B extends Thread {
  @Override
  public void run() {
    for (int i = 0; i < 7; ++i) {
      try {
        Thread.sleep(1000);
      }catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println(this.getClass().getName() +' ' + i);
    }
  }
}

  

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 T {
  public static void main(String[] args) throws InterruptedException {
    Thread t =new Thread(new B());
    int c=0;
    do {
      System.out.println("Main " + ++c);
      if (c == 3) {
        t.start();
        t.join();
      }
      Thread.sleep(500);
    }while (c != 6);
  }
}
 
class B implements Runnable{
  private int c;
  @Override
  public void run() {
    do {
      System.out.println(this.getClass().getName() + ++c);
      try {
        Thread.sleep(500);
      }catch (InterruptedException e) {
        e.printStackTrace();
      }
    }while (c != 5);  
  }
}

  

Daemon Thread

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
package ersatz.thread;
public class T {
  public static void main(String[] args) throws InterruptedException {
    DaemonThread daemonThread =new DaemonThread();
    daemonThread.setDaemon(true);
    daemonThread.start();
    int c=0;
    do {
      System.out.println(T.class +" "+c);
      Thread.sleep(500);
    }while (c++ != 4);
  }
}
 
class DaemonThread extends Thread{
  @Override
  public void run() {
    for (; ; ) {
      System.out.println(this.getName());
      try {
        Thread.sleep(500);
      }catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}

    

查看线程状态

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
package ersatz.thread;
 
public class T {
  public static void main(String[] args) throws InterruptedException {
    B b =new B();
    System.out.println(b.getName() +" state: " + b.getState());
    b.start();
    while (b.getState() != Thread.State.TERMINATED) {
      System.out.println(b.getName() +" state: " + b.getState());
      Thread.sleep(500);
    }
    System.out.println(b.getName() +" state: " + b.getState());
  }
}
 
class B extends Thread {
  @Override
  public void run() {
    while (true) {
      for (int i = 0; i < 10; ++i) {
        System.out.println(this.getClass().getName() +' ' + i);
        try {
          Thread.sleep(1000);
        }catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
      break;
    }
  }
}

  

服务器评测 http://www.cncsto.com/ 

服务器测评 http://www.cncsto.com/ 

站长资源 https://www.cscnn.com/



文章分类
后端
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐