進(jìn)程通信java代碼 進(jìn)程間通信java

java 進(jìn)程間通訊的有幾種方法?

進(jìn)程間通信的方法主要有以下幾種:

創(chuàng)新互聯(lián)專業(yè)IDC數(shù)據(jù)服務(wù)器托管提供商,專業(yè)提供成都服務(wù)器托管,服務(wù)器租用,樂山服務(wù)器托管,樂山服務(wù)器托管,成都多線服務(wù)器托管等服務(wù)器托管服務(wù)。

(1)管道(Pipe):管道可用于具有親緣關(guān)系進(jìn)程間的通信,允許一個(gè)進(jìn)程和另一個(gè)與它有共同祖先的進(jìn)程之間進(jìn)行通信。

(2)命名管道(named pipe):命名管道克服了管道沒有名字的限制,因此,除具有管道所具有的功能外,它還允許無親緣關(guān) 系?進(jìn)程間的通信。命名管道在文件系統(tǒng)中有對(duì)應(yīng)的文件名。命名管道通過命令mkfifo或系統(tǒng)調(diào)用mkfifo來創(chuàng)建。

(3)信號(hào)(Signal):信號(hào)是比較復(fù)雜的通信方式,用于通知接受進(jìn)程有某種事件發(fā)生,除了用于進(jìn)程間通信外,進(jìn)程還可以發(fā)送 信號(hào)給進(jìn)程本身;linux除了支持Unix早期信號(hào)語(yǔ)義函數(shù)sigal外,還支持語(yǔ)義符合Posix.1標(biāo)準(zhǔn)的信號(hào)函數(shù)sigaction(實(shí)際上,該函數(shù)是基于BSD的,BSD為了實(shí)現(xiàn)可靠信號(hào)機(jī)制,又能夠統(tǒng)一對(duì)外接口,用sigaction函數(shù)重新實(shí)現(xiàn)了signal函數(shù))。

(4)消息(Message)隊(duì)列:消息隊(duì)列是消息的鏈接表,包括Posix消息隊(duì)列system V消息隊(duì)列。有足夠權(quán)限的進(jìn)程可以向隊(duì)列中添加消息,被賦予讀權(quán)限的進(jìn)程則可以讀走隊(duì)列中的消息。消息隊(duì)列克服了信號(hào)承載信息量少,管道只能承載無格式字節(jié)流以及緩沖區(qū)大小受限等缺

(5)共享內(nèi)存:使得多個(gè)進(jìn)程可以訪問同一塊內(nèi)存空間,是最快的可用IPC形式。是針對(duì)其他通信機(jī)制運(yùn)行效率較低而設(shè)計(jì)的。往往與其它通信機(jī)制,如信號(hào)量結(jié)合使用,來達(dá)到進(jìn)程間的同步及互斥。

(6)內(nèi)存映射(mapped memory):內(nèi)存映射允許任何多個(gè)進(jìn)程間通信,每一個(gè)使用該機(jī)制的進(jìn)程通過把一個(gè)共享的文件映射到自己的進(jìn)程地址空間來實(shí)現(xiàn)它。

(7)信號(hào)量(semaphore):主要作為進(jìn)程間以及同一進(jìn)程不同線程之間的同步手段。

(8)套接口(Socket):更為一般的進(jìn)程間通信機(jī)制,可用于不同機(jī)器之間的進(jìn)程間通信。起初是由Unix系統(tǒng)的BSD分支開發(fā)出來的,但現(xiàn)在一般可以移植到其它類Unix系統(tǒng)上:Linux和System V的變種都支持套接字。

而在java中我們實(shí)現(xiàn)多線程間通信則主要采用"共享變量"和"管道流"這兩種方法

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

方法二 通過管道流

其中方法一有兩種實(shí)現(xiàn)方法,即

方法一a)通過內(nèi)部類實(shí)現(xiàn)線程的共享變量

代碼如下:

public class Innersharethread {

public static void main(String[] args) {

Mythread mythread = new Mythread();

mythread.getThread().start();

mythread.getThread().start();

mythread.getThread().start();

mythread.getThread().start();

}

}

class Mythread {

int index = 0;

private class InnerThread extends Thread {

public synchronized void run() {

while (true) {

System.out.println(Thread.currentThread().getName()

+ "is running and index is " + index++);

}

}

}

public Thread getThread() {

return new InnerThread();

}

}

/**

* 通過內(nèi)部類實(shí)現(xiàn)線程的共享變量

*

*/

public class Innersharethread {

public static void main(String[] args) {

Mythread mythread = new Mythread();

mythread.getThread().start();

mythread.getThread().start();

mythread.getThread().start();

mythread.getThread().start();

}

}

class Mythread {

int index = 0;

private class InnerThread extends Thread {

public synchronized void run() {

while (true) {

System.out.println(Thread.currentThread().getName()

+ "is running and index is " + index++);

}

}

}

public Thread getThread() {

return new InnerThread();

}

}

b)通過實(shí)現(xiàn)Runnable接口實(shí)現(xiàn)線程的共享變量

代碼如下:

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();

}

}

/* 實(shí)現(xiàn)Runnable接口 */

class Mythread implements Runnable {

int index = 0;

public synchronized void run() {

while (true)

System.out.println(Thread.currentThread().getName() + "is running and

the index is " + index++);

}

}

/**

* 通過實(shí)現(xiàn)Runnable接口實(shí)現(xiàn)線程的共享變量

*/

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();

}

}

/* 實(shí)現(xiàn)Runnable接口 */

class Mythread implements Runnable {

int index = 0;

public synchronized void run() {

while (true)

System.out.println(Thread.currentThread().getName() + "is running and

the index is " + index++);

}

}

方法二(通過管道流):

代碼如下:

public class CommunicateWhitPiping {

public static void main(String[] args) {

/**

* 創(chuàng)建管道輸出流

*/

PipedOutputStream pos = new PipedOutputStream();

/**

* 創(chuàng)建管道輸入流

*/

PipedInputStream pis = new PipedInputStream();

try {

/**

* 將管道輸入流與輸出流連接 此過程也可通過重載的構(gòu)造函數(shù)來實(shí)現(xiàn)

*/

pos.connect(pis);

} catch (IOException e) {

e.printStackTrace();

}

/**

* 創(chuàng)建生產(chǎn)者線程

*/

Producer p = new Producer(pos);

/**

* 創(chuàng)建消費(fèi)者線程

*/

Consumer c = new Consumer(pis);

/**

* 啟動(dòng)線程

*/

p.start();

c.start();

}

}

/**

* 生產(chǎn)者線程(與一個(gè)管道輸入流相關(guān)聯(lián))

*

*/

class Producer extends Thread {

private PipedOutputStream pos;

public Producer(PipedOutputStream pos) {

this.pos = pos;

}

public void run() {

int i = 8;

try {

pos.write(i);

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 消費(fèi)者線程(與一個(gè)管道輸入流相關(guān)聯(lián))

*

*/

class Consumer extends Thread {

private PipedInputStream pis;

public Consumer(PipedInputStream pis) {

this.pis = pis;

}

public void run() {

try {

System.out.println(pis.read());

} catch (IOException e) {

e.printStackTrace();

}

}

}

java如何實(shí)現(xiàn)進(jìn)程間的通信

傳統(tǒng)的進(jìn)程間通信的方式有大致如下幾種:

(1) 管道(PIPE)

(2) 命名管道(FIFO)

(3) 信號(hào)量(Semphore)

(4) 消息隊(duì)列(MessageQueue)

(5) 共享內(nèi)存(SharedMemory)

(6) Socket

Java如何支持進(jìn)程間通信。我們把Java進(jìn)程理解為JVM進(jìn)程。很明顯,傳統(tǒng)的這些大部分技術(shù)是無法被我們的應(yīng)用程序利用了(這些進(jìn)程間通信都是靠系統(tǒng)調(diào)用來實(shí)現(xiàn)的)。但是Java也有很多方法可以進(jìn)行進(jìn)程間通信的。

除了上面提到的Socket之外,當(dāng)然首選的IPC可以使用Rmi,或者Corba也可以。另外Java nio的MappedByteBuffer也可以通過內(nèi)存映射文件來實(shí)現(xiàn)進(jìn)程間通信(共享內(nèi)存)。

java 進(jìn)程間的通信 請(qǐng)高手指點(diǎn),謝謝

br.readLine()``這個(gè)會(huì)使程序阻塞``讀不到東西``就會(huì)一直在哪里等著``直到讀到之后``才會(huì)結(jié)束`

本文題目:進(jìn)程通信java代碼 進(jìn)程間通信java
當(dāng)前URL:http://bm7419.com/article30/ddccipo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、建站公司App開發(fā)、用戶體驗(yàn)、App設(shè)計(jì)、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)