多线程的实现步骤

方式2:实现Runnable 接口

  • 定义一个MyRunnable类实现Runnable 接口
  • 在MyRunnable类中重写run()方法
  • 创建MyRunnable类对象
  • 创建Thread类对象,把MyRunnable对象作为构造方法的参数
  • 启动线程

    1. /*
    2. 方式2:实现Runnable 接口
    3. ● 定义一个MyRunnable类实现Runnable 接口
    4. ● 在MyRunnable类中重写run()方法
    5. ● 创建MyRunnable类对象
    6. ● 创建Thread类对象,把MyRunnable对象作为构造方法的参数
    7. ● 启动线程
    8. */
    9. //1、定义一个MyRunnable类实现Runnable 接口
    10. class MyRunnable implements Runnable{
    11. //2、重写run()方法
    12. public void run(){
    13. for(int i=0;i<10;i++)
    14. System.out.println(Thread.currentThread().getName()+":"+i+"次 优先级"+Thread.currentThread().getPriority());
    15. try {
    16. Thread.sleep(1000);//休眠1000ms
    17. } catch (InterruptedException e) {
    18. throw new RuntimeException(e);
    19. }
    20. }
    21. }
    22. public class MyRunnableDemo {
    23. public static void main(String[] args) {
    24. //3、创建Runnable对象
    25. MyRunnable my=new MyRunnable();
    26. //4、创建Thread类对象,把MyRunnable对象作为构造方法的参数
    27. //Thread(Runnable target)
    28. Thread t1=new Thread(my);//执行默认参数名
    29. Thread t2=new Thread(my);
    30. //Thread(Runnable target,String name)
    31. Thread t3=new Thread(my,"高铁");//设置线程名称
    32. Thread t4=new Thread(my,"火车");
    33. //5、启动线程
    34. t1.start();//未设置优先级,默认为5
    35. try {
    36. t1.join(); //t1线程执行完毕之后才能执行其他
    37. } catch (InterruptedException e) {
    38. throw new RuntimeException(e);
    39. }
    40. //设置优先级
    41. t2.setPriority(10);
    42. t3.setPriority(1);
    43. t4.setPriority(5);
    44. t2.start();
    45. t3.start();
    46. t4.start();
    47. }
    48. }
    49. Thread-0:0 优先级5
    50. Thread-0:1 优先级5
    51. Thread-0:2 优先级5
    52. Thread-0:3 优先级5
    53. Thread-0:4 优先级5
    54. Thread-0:5 优先级5
    55. Thread-0:6 优先级5
    56. Thread-0:7 优先级5
    57. Thread-0:8 优先级5
    58. Thread-0:9 优先级5
    59. Thread-1:0 优先级10
    60. Thread-1:1 优先级10
    61. Thread-1:2 优先级10
    62. 高铁:0 优先级1
    63. Thread-1:3 优先级10
    64. 火车:0 优先级5
    65. Thread-1:4 优先级10
    66. 高铁:1 优先级1
    67. 高铁:2 优先级1
    68. Thread-1:5 优先级10
    69. 火车:1 优先级5
    70. Thread-1:6 优先级10
    71. 高铁:3 优先级1
    72. Thread-1:7 优先级10
    73. 火车:2 优先级5
    74. 高铁:4 优先级1
    75. 高铁:5 优先级1
    76. Thread-1:8 优先级10
    77. 高铁:6 优先级1
    78. 火车:3 优先级5
    79. 高铁:7 优先级1
    80. Thread-1:9 优先级10
    81. 高铁:8 优先级1
    82. 火车:4 优先级5
    83. 火车:5 优先级5
    84. 高铁:9 优先级1
    85. 火车:6 优先级5
    86. 火车:7 优先级5
    87. 火车:8 优先级5
    88. 火车:9 优先级5

    通过 Thread 类的构造函数
    public Thread(Runnable target)
    可以将一个Runnable 接口对象传递给线程,线程在调度时将自动调用Runnable 接口对象的run方法。

    1. public class Thread implements Runnable {
    2. private Runnable target;
    3. public Thread() {…}
    4. public Thread( Runnable target ) {…}
    5. public void run()
    6. {
    7. if (target!=null)
    8. target.run(); 转而执行Runnabletarget对象的run()方法
    9. }
    10. }

    ```java import java.util.*; class TimePrinter implements Runnable { int pauseTime; // 中间休息时间 String name; // 名称标识 public TimePrinter(int x , String n) {

    1. pauseTime = x;
    2. name = n;

    } public void run() {

    1. while(true) {
    2. System.out.println(name + ": " +Calendar.getInstance().getTime());
    3. try {
    4. Thread.sleep(pauseTime);
    5. } catch (InterruptedException e) {
    6. throw new RuntimeException(e);
    7. }
    8. }

    } public static void main(String args[ ]) {

    1. Thread tp1 = new Thread( new TimePrinter(1000, "Fast") ); 【思考】此处为什么用Thread ?用TimePrinterRunnable可以吗?暂时理解TimePrinter没有start()方法
    2. tp1.start();
    3. Thread tp2 = new Thread(new TimePrinter(3000, "Slow"));
    4. tp2.start();

    } }

Slow: Sun May 15 16:37:52 CST 2022 Fast: Sun May 15 16:37:52 CST 2022 Fast: Sun May 15 16:37:53 CST 2022 Fast: Sun May 15 16:37:54 CST 2022 Slow: Sun May 15 16:37:55 CST 2022 Fast: Sun May 15 16:37:55 CST 2022 Fast: Sun May 15 16:37:56 CST 2022 Fast: Sun May 15 16:37:57 CST 2022 Slow: Sun May 15 16:37:58 CST 2022 Fast: Sun May 15 16:37:58 CST 2022 Fast: Sun May 15 16:37:59 CST 2022 Fast: Sun May 15 16:38:00 CST 2022 Slow: Sun May 15 16:38:01 CST 2022 Fast: Sun May 15 16:38:01 CST 2022 ……

  1. ```java
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class Winning extends Frame implements Runnable {
  5. String phoneNumber[ ] = { "15031204532", "13014156678", "13870953214", "13943123322", "18114156528" };
  6. TextArea disp = new TextArea(4, 50); // 用来显示滚动号码
  7. int pos = 0; //记录滚动到的索引位置
  8. boolean flag; // 控制线程停止的标记变量
  9. Button onoff; //启动、停止按钮
  10. public static void main(String[ ] args) {
  11. new Winning( );
  12. }
  13. public Winning( ) {
  14. add("Center", disp);
  15. onoff = new Button("begin");
  16. add("South", onoff);
  17. onoff.addActionListener(new ActionListener( ) {
  18. public void actionPerformed(ActionEvent e)
  19. {
  20. if (e.getActionCommand( ).equals("begin"))
  21. {
  22. flag = true;
  23. onoff.setLabel("end");
  24. (new Thread(Winning.this)).start( );
  25. }
  26. else
  27. {
  28. flag = false; //设置线程停止标记
  29. onoff.setLabel("begin");
  30. }
  31. }
  32. });
  33. setSize(200, 100);
  34. setVisible(true);
  35. }
  36. public void run( )
  37. {
  38. while (flag)
  39. {
  40. int n = phoneNumber.length;
  41. pos = (int) (Math.random( ) * n);
  42. String message = phoneNumber[pos] + "\n"+ phoneNumber[(pos + 1) % n];
  43. disp.setText(message);
  44. }
  45. }
  46. }

image.png