<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>

            Java實現在不同線程中運行的代碼實例詳解

            時間:2025-11-11 00:18:43 java語言

            Java實現在不同線程中運行的代碼實例詳解

              本文實例講述了Java實現在不同線程中運行的代碼。下面是由百分網小編為大家整理的Java實現在不同線程中運行的代碼實例詳解,喜歡的可以收藏一下!了解更多詳情資訊,請關注應屆畢業生考試網!

              start()方法開始為一個線程分配CPU時間,這導致對run()方法的調用。

              代碼1

              package Threads;

              /**

              * Created by Frank

              */

              public class ThreadsDemo1 extends Thread {private String msg;

              private int count;

              public ThreadsDemo1(final String msg, int n) {this.msg = msg;

              count = n;

              setName(msg + " runner Thread");

              }

              public void run() {

              while (count-- > 0) {

              System.out.println(msg);

              try {

              Thread.sleep(100);

              } catch (InterruptedException e) {

              return;

              }

              }

              System.out.println(msg + " all done.");

              }

              public static void main(String[] args) {

              new ThreadsDemo1("Hello from X", 10).start();new ThreadsDemo1("Hello from Y", 15).start();}

              }

              代碼2:

              package Threads;

              /**

              * Created by Frank

              */

              public class ThreadsDemo2 implements Runnable {private String msg;

              private Thread t;

              private int count;

              public static void main(String[] args) {

              new ThreadsDemo2("Hello from X", 10);

              new ThreadsDemo2("Hello from Y", 15);

              }

              public ThreadsDemo2(String m, int n) {

              this.msg = m;

              count = n;

              t = new Thread(this);

              t.setName(msg + "runner Thread");

              t.start();

              }

              public void run() {

              while (count-- > 0) {

              System.out.println(msg);

              try {

              Thread.sleep(100);

              } catch (InterruptedException e) {

              return;

              }

              }

              System.out.println(msg + " all done.");

              }

              }

              代碼3:

              package Threads;

              /**

              * Created by Frank

              */

              public class ThreadsDemo3 {

              private int count;

              public static void main(String[] args) {

              new ThreadsDemo3("Hello from X", 10);

              new ThreadsDemo3("Hello from Y", 15);

              }

              public ThreadsDemo3(final String msg, int n) {this.count = n;

              Thread t = new Thread(new Runnable() {

              public void run() {

              while (count-- > 0) {

              System.out.println(msg);

              try {

              Thread.sleep(100);

              } catch (InterruptedException e) {

              return;

              }

              }

              System.out.println(msg + " all done.");

              }

              });

              t.setName(msg + " runner Thread");

              t.start();

              }

              }

              eclipse運行結果如下:

            【Java實現在不同線程中運行的代碼實例詳解】相關文章:

            java中通用的線程池實例代碼12-18

            Java裝箱與拆箱詳解(附實例代碼)02-11

            如何創建并運行Java線程01-11

            java多線程-線程通信實例詳細解讀02-03

            Java基礎從代碼到運行12-29

            java從代碼到運行的過程10-30

            Java優先級線程代碼示例03-21

            詳解java線程的生命周期08-08

            java Runnable接口創建線程詳解02-15

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看