最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)在不同線程中運(yùn)行的代碼實(shí)例

 更新時(shí)間:2017年04月12日 11:57:02   作者:FrankYou  
這篇文章主要介紹了Java實(shí)現(xiàn)在不同線程中運(yùn)行的代碼,結(jié)合具體實(shí)例形式分析了java多線程操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)在不同線程中運(yùn)行的代碼。分享給大家供大家參考,具體如下:

start()方法開始為一個(gè)線程分配CPU時(shí)間,這導(dǎo)致對(duì)run()方法的調(diào)用。

代碼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運(yùn)行結(jié)果如下:

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

乡宁县| 敦煌市| 磐石市| 禹州市| 双峰县| 蒲江县| 兰州市| 宜兰市| 巴彦淖尔市| 江油市| 察雅县| 宾川县| 贞丰县| 尉犁县| 南投县| 日喀则市| 射洪县| 泰和县| 新昌县| 平湖市| 海宁市| 怀远县| 上高县| 宝应县| 石景山区| 余庆县| 灵宝市| 中卫市| 仙桃市| 肃北| 油尖旺区| 米林县| 瓦房店市| 昌宁县| 射阳县| 城固县| 河西区| 宁波市| 普格县| 扎鲁特旗| 始兴县|