欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

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

創(chuàng)新互聯(lián)專注于灞橋企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),電子商務(wù)商城網(wǎng)站建設(shè)。灞橋網(wǎng)站建設(shè)公司,為灞橋等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

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

(2)命名管道(named pipe):命名管道克服了管道沒(méi)有名字的限制,因此,除具有管道所具有的功能外,它還允許無(wú)親緣關(guān) 系?進(jìn)程間的通信。命名管道在文件系統(tǒng)中有對(duì)應(yīng)的文件名。命名管道通過(guò)命令mkfifo或系統(tǒng)調(diào)用mkfifo來(lái)創(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)承載信息量少,管道只能承載無(wú)格式字節(jié)流以及緩沖區(qū)大小受限等缺

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

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

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

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

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

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

方法二 通過(guò)管道流

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

方法一a)通過(guò)內(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();

}

}

/**

* 通過(guò)內(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)通過(guò)實(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++);

}

}

/**

* 通過(guò)實(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++);

}

}

方法二(通過(guò)管道流):

代碼如下:

public class CommunicateWhitPiping {

public static void main(String[] args) {

/**

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

*/

PipedOutputStream pos = new PipedOutputStream();

/**

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

*/

PipedInputStream pis = new PipedInputStream();

try {

/**

* 將管道輸入流與輸出流連接 此過(guò)程也可通過(guò)重載的構(gòu)造函數(shù)來(lái)實(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線程的經(jīng)典代碼

package threadgroup;

class ThreadDemo3 extends Thread {

private String name;

private int delay;

public ThreadDemo3(String sname, int i_delay) {

name = sname;

delay = i_delay;

}

public void run() {

try {

sleep(delay);

} catch (InterruptedException e) {

}

System.out.println("多線程測(cè)試!\n" + name + "\n" + delay);

}

}

public class testMyThread {

public static void main(String[] args) {

ThreadDemo3 th1,th2,th3;

th1 = new ThreadDemo3("線程1", (int) (Math.random() * 900));

th2 = new ThreadDemo3("線程2", (int) (Math.random() * 900));

th3 = new ThreadDemo3("線程3", (int) (Math.random() * 900));

th1.start();

th2.start();

th3.start();

}

}

package threadgroup;

public class threadDemo {

public static void main(String[] args) {

Thread t = Thread.currentThread();

t.setName("你好嗎?");

System.out.println("正在進(jìn)行的Thread是:" + t);

try {

for (int i = 0; i 5; i++) {

System.out.println("我不叫穆繼超" + i);

Thread.sleep(3000);

}

} catch (Exception e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

}

}

package threadgroup;

public class threadDemo2 implements Runnable {

public threadDemo2() {

Thread t1 = Thread.currentThread();

t1.setName("第一個(gè)主進(jìn)程");

System.out.println("正在運(yùn)行" + t1);

Thread t2 = new Thread(this, "");

System.out.println("在創(chuàng)建一個(gè)進(jìn)程");

t2.start();

try {

System.out.println("使他進(jìn)入第一個(gè)睡眠狀態(tài)");

Thread.sleep(2000);

} catch (InterruptedException e) {

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第一個(gè)進(jìn)程");

}

public void run() {

try {

for (int i = 0; i 5; i++) {

System.out.println("進(jìn)程" + i);

Thread.sleep(3000);

}

} catch (InterruptedException e) {

// TODO: handle exception

System.out.println("Thread has wrong" + e.getMessage());

}

System.out.println("退出第二個(gè)進(jìn)程");

}

public static void main(String[] args) {

new threadDemo2();

}

}

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

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

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ù)是無(wú)法被我們的應(yīng)用程序利用了(這些進(jìn)程間通信都是靠系統(tǒng)調(diào)用來(lái)實(shí)現(xiàn)的)。但是Java也有很多方法可以進(jìn)行進(jìn)程間通信的。

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

分享名稱:java進(jìn)程通信代碼,Java進(jìn)程通信
當(dāng)前路徑:http://chinadenli.net/article46/hdojhg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、外貿(mào)建站網(wǎng)站營(yíng)銷、面包屑導(dǎo)航、定制開發(fā)、云服務(wù)器

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)