熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

java間的線程通信

2022-06-13   來源: Java高級技術 

  不同線程間進行通信通常有兩種簡單方法

  方法一 通過訪問共享變量的方式(注需要處理同步問題)

  方法二 通過管道流

  其中方法一有兩種實現方法

  方法一a)通過內部類實現線程的共享變量

  代碼如下

  Java代碼

  /**

  * 通過內部類實現線程的共享變量

  *

  */

  public class Innersharethread {

  public static void main(String[] args) {

  Mythread mythread = new Mythread()

  mythreadgetThread()start()

  mythreadgetThread()start()

  mythreadgetThread()start()

  mythreadgetThread()start()

  }

  }

  class Mythread {

  int index = ;

  private class InnerThread extends Thread {

  public synchronized void run() {

  while (true) {

  Systemoutprintln(ThreadcurrentThread()getName()

  + is running and index is + index++)

  }

  }

  }

  public Thread getThread() {

  return new InnerThread()

  }

  }

  /**

  * 通過內部類實現線程的共享變量

  *

  */

  public class Innersharethread {

  public static void main(String[] args) {

  Mythread mythread = new Mythread()

  mythreadgetThread()start()

  mythreadgetThread()start()

  mythreadgetThread()start()

  mythreadgetThread()start()

  }

  }

  class Mythread {

  int index = ;

  private class InnerThread extends Thread {

  public synchronized void run() {

  while (true) {

  Systemoutprintln(ThreadcurrentThread()getName()

  + is running and index is + index++)

  }

  }

  }

  public Thread getThread() {

  return new InnerThread()

  }

  }

  方法二b)通過實現Runnable接口實現線程的共享變量

  代碼如下

  Java代碼

  /**

  * 通過實現Runnable接口實現線程的共享變量

  * @author Administrator

  *

  */

  public class Interfacaesharethread {

  public static void main(String[] args) {

  Mythread mythread = new Mythread()

  new Thread(mythread)start()

  new Thread(mythread)start()

  new Thread(mythread)start()

  new Thread(mythread)start()

  }

  }

  /* 實現Runnable接口 */

  class Mythread implements Runnable {

  int index = ;

  public synchronized void run() {

  while (true)

  Systemoutprintln(ThreadcurrentThread()getName()

  + is running and the index is + index++)

  }

  }

  /**

  * 通過實現Runnable接口實現線程的共享變量

  * @author Administrator

  *

  */

  public class Interfacaesharethread {

  public static void main(String[] args) {

  Mythread mythread = new Mythread()

  new Thread(mythread)start()

  new Thread(mythread)start()

  new Thread(mythread)start()

  new Thread(mythread)start()

  }

  }

  /* 實現Runnable接口 */

  class Mythread implements Runnable {

  int index = ;

  public synchronized void run() {

  while (true)

  Systemoutprintln(ThreadcurrentThread()getName()

  + is running and the index is + index++)

  }

  }

  方法二

  代碼如下

  Java代碼

  import javaioIOException;

  import javaioPipedInputStream;

  import javaioPipedOutputStream;

  public class CommunicateWhitPiping {

  public static void main(String[] args) {

  /**

  * 創建管道輸出流

  */

  PipedOutputStream pos = new PipedOutputStream()

  /**

  * 創建管道輸入流

  */

  PipedInputStream pis = new PipedInputStream()

  try {

  /**

  * 將管道輸入流與輸出流連接

  * 此過程也可通過重載的構造函數來實現

  */

  nnect(pis)

  } catch (IOException e) {

  eprintStackTrace()

  }

  /**

  * 創建生產者線程

  */

  Producer p = new Producer(pos)

  /**

  * 創建消費者線程

  */

  Consumer c = new Consumer(pis)

  /**

  * 啟動線程

  */

  pstart()

  cstart()

  }

  }

  /**

  * 生產者線程(與一個管道輸入流相關聯)

  *

  */

  class Producer extends Thread {

  private PipedOutputStream pos;

  public Producer(PipedOutputStream pos) {

  thispos = pos;

  }

  public void run() {

  int i = ;

  try {

  poswrite(i)

  } catch (IOException e) {

  eprintStackTrace()

  }

  }

  }

  /**

  * 消費者線程(與一個管道輸入流相關聯)

  *

  */

  class Consumer extends Thread {

  private PipedInputStream pis;

  public Consumer(PipedInputStream pis)

  {

  thispis = pis;

  }

  public void run() {

  try {

  Systemoutprintln(pisread())

  } catch (IOException e) {

  eprintStackTrace()

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27464.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.