A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 徐大鹏 中级黑马   /  2012-11-26 23:24  /  1234 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class ThreeTest extends Thread {
  2. private String name;
  3. private int delay;

  4. public ThreeTest(String sname, int i_delay) {
  5. name = sname;
  6. delay = i_delay;
  7. }

  8. public void run() {
  9. try {
  10. sleep(delay);
  11. } catch (InterruptedException e) {

  12. }
  13. System.out.println("多线程测试!\n" + name + "\n" + delay);
  14. }
  15. }

  16. class testMyThread {
  17. public static void main(String[] args) {
  18. ThreeTest th1, th2, th3;
  19. th1 = new ThreeTest("线程1", (int) (Math.random() * 900));
  20. th2 = new ThreeTest("线程2", (int) (Math.random() * 900));
  21. th3 = new ThreeTest("线程3", (int) (Math.random() * 900));
  22. th1.start();
  23. th2.start();
  24. th3.start();
  25. }
  26. }

  27. class threadDemo {
  28. public static void main(String[] args) {
  29. Thread t = Thread.currentThread();
  30. t.setName("你好吗?");
  31. System.out.println("正在进行的Thread是:" + t);
  32. try {
  33. for (int i = 0; i < 5; i++) {
  34. System.out.println("我不叫穆继超" + i);
  35. Thread.sleep(3000);
  36. }
  37. } catch (Exception e) { // TODO: handle exception
  38. System.out.println("Thread has wrong" + e.getMessage());
  39. }
  40. }
  41. }

  42. class threadDemo2 implements Runnable {
  43. public threadDemo2() {
  44. Thread t1 = Thread.currentThread();
  45. t1.setName("第一个主进程");
  46. System.out.println("正在运行" + t1);
  47. Thread t2 = new Thread(this, "");
  48. System.out.println("在创建一个进程");
  49. t2.start();
  50. try {
  51. System.out.println("使他进入第一个睡眠状态");
  52. Thread.sleep(2000);
  53. } catch (InterruptedException e) {
  54. System.out.println("Thread has wrong" + e.getMessage());
  55. }
  56. System.out.println("退出第一个进程");
  57. }

  58. public void run() {
  59. try {
  60. for (int i = 0; i < 5; i++) {
  61. System.out.println("进程" + i);
  62. Thread.sleep(3000);
  63. }
  64. } catch (InterruptedException e) { // TODO: handle exception
  65. System.out.println("Thread has wrong" + e.getMessage());
  66. }
  67. System.out.println("退出第二个进程");
  68. }

  69. public static void main(String[] args) {
  70. new threadDemo2();
  71. }
  72. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马