您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AJDBThreads.java 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //package debugger;
  2. public class AJDBThreads {
  3. public static void main(String[] args) {
  4. new ThreadForker().go();
  5. }
  6. public static String currentThread = "none";
  7. }
  8. class ThreadForker {
  9. public void go() {
  10. fork(1000);
  11. fork(500);
  12. fork(200);
  13. fork(100);
  14. }
  15. void fork(long sleep) {
  16. new NamedThread(sleep).start();
  17. }
  18. }
  19. class NamedThread implements Runnable {
  20. private long sleep;
  21. private String name;
  22. private Thread thread;
  23. private int num = 0;
  24. public NamedThread(long sleep) {
  25. this.sleep = sleep;
  26. }
  27. public void start() {
  28. if (thread == null) {
  29. thread = new Thread(this);
  30. name = thread.getName();
  31. thread.start();
  32. }
  33. }
  34. public void run() {
  35. while (true) {
  36. AJDBThreads.currentThread = name;
  37. System.out.println("\n********** " + AJDBThreads.currentThread + ":" + (num++) + "\n");
  38. try {
  39. Thread.sleep(sleep);
  40. } catch (Exception e) {
  41. }
  42. }
  43. }
  44. }