「java中通讯」java通讯录

博主:adminadmin 2023-01-01 21:21:08 808

本篇文章给大家谈谈java中通讯,以及java通讯录对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 进程间通讯的有几种方法

 JAVA进程间通信的方法主要有以下几种:

(1)管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先的进程之间进行通信。

(2)命名管道(named pipe):命名管道克服了管道没有名字的限制,除具有管道所具有的功能外,它还允许无亲缘关系进程间的通信。

(3)信号(Signal):信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送 信号给进程本身。

(4)消息(Message)队列:消息队列是消息的链接表,包括Posix消息队列system V消息队列。

(5)共享内存:使得多个进程可以访问同一块内存空间,是最快的可用IPC形式。是针对其他通信机制运行效率较低而设计的。

(6)内存映射(mapped memory):内存映射允许任何多个进程间通信,每一个使用该机制的进程通过把一个共享的文件映射到自己的进程地址空间来实现它。

(7)信号量(semaphore):主要作为进程间以及同一进程不同线程之间的同步手段。

(8)套接口(Socket):更为一般的进程间通信机制,可用于不同机器之间的进程间通信。

Java 中利用管道实现线程间的通讯

在Java 语言中 提供了各种各样的输入输出流(stream) 使我们能够很方便的对数据进行操作 其中 管道(pipe)流是一种特殊的流 用于在不同线程(threads)间直接传送数据 一个线程发送数据到输出管道 另一个线程从输入管道中读数据 通过使用管道 实现不同线程间的通讯 无需求助于类似临时文件之类的东西 本文在简要介绍管道的基本概念后 将以一个具体的实例pipeapp加以详细说明 .管道的创建与使用Java提供了两个特殊的专门的类专门用于处理管道 它们就是pipedinputstream类和pipeoutputstream类 Pipedinputstream代表了数据在管道中的输出端 也就是线程向管道读数据的一端 pipeoutputstream代表了数据在管道中的输入端 也就是线程向管道写数据的一端 这两个类一起使用可以提供数据的管道流 为了创建一个管道流 我们必须首先创建一个pipeoutstream对象 然后 创建pipeinputstream对象 实例如下 pipeout= new pipedyoutstream();pipein= new pipedputsteam(pipepout);一旦创建了一个管道后 就可以象操作文件一样对管道进行数据的读写 .演示程序 pipeapp应用程序由三个程序组成 主线程(pipeapp Java)及由主线程启动的两个二级线程(ythread Java和zthread Java) 它们使用管道来处理数据 程序从一个内容为一行一行 x 字母的 input txt 文件中读取数据 使用管道传输数据 第一次是利用线程ythread将数据 x 转换为 y 最后利用线程zthread将 y 转换为 z 之后 程序在屏幕上显示修改后的数据 主线程 (pipeapp Java)在main()方法中 程序首先创建一个应用对象 pipeapp pipeapp=new pipeapp();由于程序中流操作都需要使用IOException异常处理 所以设置了一个try块 在try中 为了从源文件中读取数据 程序为 input txt 文件创建了一个输入流Xfileln :fileinputstream xfileln= new fileinputstream( input txt );新的输入流传递给changetoy()方法 让线程ythread能读取该文件 inputstream ylnpipe =pipeapp changetoy(xfileln);changetoy()方法创建将输入数据 x 改变到 y 的线程ythread 并返回该线程的输入管道 inputstream zlnpipe = pipeapp changetoz(ylnpipe);changetoz()方法启动将数据从 y 改变到 z 的线程zehread 主程序将使用从changetoz()返回的输入管道 得到以修改的数据 然后 程序将管道输入流定位到datainputstream对象 使程序能够使用readline()方法读取数据 datainputstream inputstream = new datainputstream(zlnpiepe);创建了输入流以后 程序就可以以行一行的读取数据病显示在屏幕上 String str= inputstream readline();While(str!=null){system out println(str); str=inputstream readline();} 显示完成之后 程序关闭输入流 inputstream close();changetoy()方法 changetoy()方法首先通过传递一个参数inputstream给datainputstream对象来定位资源的输入流 使程序能使用readline()方法从流中读取数据 datainputstream xfileln =new datainutstream(inputstream) 然后 changetoy()创建输出管道和输入管道 pipeoutstream pipeout = new pipeoutputstream();pipeinputstream pipeln = new pipedinputsteam(pipeout); 为了能够使用println()方法输出修改的后的文本行到管道 程序将输出管道定位到printstream对象 printstream printstream = new printstream(pipeout);现在 程序可以创建将数据从x改变到y的线程 该线程是ythread类的一个对象 他传递两个参数 输入文件(xfileln)和输出管道(调用printstream) ythread ythread =new thread(xfileln printstream);之后 程序启动线程 changetoz()方法changetoz()方法与changetoy()方法很相似 他从changetoy()返回的输入流开始 datainputstream yfileln= new datainputstream(inputstream);程序创建一个新的管道 pipedoutstream pipeout = new pipedoutputstream();pipedinputstream pipeln = new pipedinputsream(pipeout ); 该线程通过这个新的管道发出修改后的数据(输入流pipeln )给主程序 源程序如下 ////pipeapp Java pipeapp的主应用程序//import Java io *class pipeapp{public static void main(string[] args){pipeapp pipeapp=new pipeapp();try{fileinputstream xfile =new fileinputstream( input txt );inputstream ylnpipe = pipeapp changetoy(xfileln);inputstream zlnpipe=pipeapp changetoz(ylnpipe);system out println();system out println( here are the results );system out pringln();datainputstream inputstream = nes datainputstream(zlnpipe);string str = inputstream readline();while (str!=null){system out println(str);str=inputstream readline();}inputstream close();}catch(exception e){system out println(e tostring());}}public inputstream changetoy(inputstream inputstream){try{datainputstream pipeout = new datainputsteam(inputstream);pipedoutstream pipeout = new pipedoutputstream();pipedlnsteam pipeln = new pipedlnputstream(pipeout);printstream printstream = new printstream(pipeout);ythread ythread = new ythread(xfileln printstream);ythread start();return pipeln;}catch(exeption e){system out println(x tostring());}return null;}public inputstream changetoz(inputstream inputsteam){try{datainputstream yfileln = new datainputstream(inputstream);pipeoutputstream pipeln = new pipedinputstream(pipeout );printrstream printstream = new printsteam(pipeout );zthread zthread = new zthread(yfileln printstream );zthread start();return pipeln ;}catch(exception e){system out println(e tostring());}return null;}} Ythread类和Zthread类由于ythread类与zthread类基本一样 在此仅以ythread为例加以说明 Ythread的构造器接收两个参数 输入的文件和第一个管道的输出端 构造器存储这两个参数作为类的数据成员  Ythread(datainputstream xfileln pringstream printstream){this xfileln = xfileln;this printstream = printstream;} 线程通过run()方法来处理数据 首先读取一行数据 确保xstring不为空的情况下循环执行 string xstring = xfileln readline();每读一行数据 完成一次转换string ystring = xstring replace( x y );然后将修改后的数据输出到管道的输出端 prinstream prinrln(ystring);为了确保所有缓冲区的数据完全进入管道的输出端 pringstram flush();循环完成后 线程关闭管道输出流  pringstram close();ythread类的源程序如下 //ythread Java//import Java io *;class ythread exteads thread{datainputstream xfileln;pringstream printstream;ythread(datainputstream xfileln pringstream printstream){this xfileln = xfileln;this printstream = printstream;}public void run(){try{string xstring = xfileln readline();while(xstring!=null){string ystring= xstring replace( x y );printstream pringln(ystring);printstream flush();xstring= xfileln readline();}printstream close();}catch{ioexception e}{system out println(e tostring());}}} pipeapp应用程序使用microsoft visual j++ 编译 lishixinzhi/Article/program/Java/gj/201311/27508

在java的web程序中怎么使用串口通讯?

最近在做java串口通讯,主要是用个人电脑通过串口从RS485读取数据,并通过crc循环冗余校验,把接收正确的数据解析,插入数据库mysql,并用SSH技术把数据库数据以表格以及图表形式显示 \x0d\x0a 思路: \x0d\x0a1.为了从RS485读取数据,由于暂时没有硬件设备,系统是win7,故采用Virtual Serial Port Drive(VSPD)这块虚拟串口软件代替。并下载sscom32.exe模拟串口通信软件。 \x0d\x0a\x0d\x0a2. 要想实现串口通信,用Java实现串口通信(windows系统下),需要用到sun提供的串javacomm20-win32.zip。其中要用到三个文件,配置如下: \x0d\x0acomm.jar放置到 JAVA_HOME/jre/lib/ext; \x0d\x0awin32com.dll放置到 JAVA_HOME/bin; \x0d\x0ajavax.comm.properties 两个地方都要放 \x0d\x0ajre/lib(也就是在JAVA文件夹下的jre),JAVA_HOME/jre/lib下 \x0d\x0a这个配置在我电脑上测试成功,也许不需要这样麻烦。注意的是,如果你使用myeclipse,因为它自带jre,你需要在它所在的jre相应位置放dll以及properties文件。 \x0d\x0a\x0d\x0a是不是感觉这个很麻烦,还有windows的限制。后来我们下载rxtx这款开源包代替了刚才的comm。不仅windows下可以,linux下也可以。使用方法很简单,配置如下: \x0d\x0a\x0d\x0aRXTXcomm.jar放到JAVA_HOME/jre/lib/ext \x0d\x0arxtxSerial.dll放到JAVA_HOME/bin \x0d\x0a如果你使用myeclipse工具,你需要把rxtxSerial.dll放到它自带的jre里。 \x0d\x0a\x0d\x0a3.新建eclipse工程,添加comm.jar或者RXTXcomm.jar包。因为javacomm20-win32.zip包里有样例SimpleRead.java,可以通过这个例子测试串口是否正确 \x0d\x0a\x0d\x0a4.接收数据正确后,根据传送接收双方的协议,采用CRC循环校验,根据传输的一方的校验函数判定是否是正确传输 \x0d\x0a\x0d\x0a5.把正确结束的数据解析,查看自己指定的通讯规则,然后解析 \x0d\x0a\x0d\x0a6.插入数据库,jdbc插入 \x0d\x0a\x0d\x0a7.数据统计,定时统计每小时,每天,每月,每年的平均值,采用quartz服务来实现。 \x0d\x0a\x0d\x0a8.建立web工程,采用hibernate3,spring3,dwr技术把数据库数据动态显示,图表采用jfreechart,以及AJAX的运用

java 进程间通讯的有几种方法?

进程间通信的方法主要有以下几种:

  (1)管道(Pipe):管道可用于具有亲缘关系进程间的通信,允许一个进程和另一个与它有共同祖先的进程之间进行通信。

  (2)命名管道(named pipe):命名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允许无亲缘关 系 进程间的通信。命名管道在文件系统中有对应的文件名。命名管道通过命令mkfifo或系统调用mkfifo来创建。

  (3)信号(Signal):信号是比较复杂的通信方式,用于通知接受进程有某种事件发生,除了用于进程间通信外,进程还可以发送 信号给进程本身;linux除了支持Unix早期信号语义函数sigal外,还支持语义符合Posix.1标准的信号函数sigaction(实际上,该函数是基于BSD的,BSD为了实现可靠信号机制,又能够统一对外接口,用sigaction函数重新实现了signal函数)。

(4)消息(Message)队列:消息队列是消息的链接表,包括Posix消息队列system V消息队列。有足够权限的进程可以向队列中添加消息,被赋予读权限的进程则可以读走队列中的消息。消息队列克服了信号承载信息量少,管道只能承载无格式字节流以及缓冲区大小受限等缺

  (5)共享内存:使得多个进程可以访问同一块内存空间,是最快的可用IPC形式。是针对其他通信机制运行效率较低而设计的。往往与其它通信机制,如信号量结合使用,来达到进程间的同步及互斥。

  (6)内存映射(mapped memory):内存映射允许任何多个进程间通信,每一个使用该机制的进程通过把一个共享的文件映射到自己的进程地址空间来实现它。

  (7)信号量(semaphore):主要作为进程间以及同一进程不同线程之间的同步手段。

  (8)套接口(Socket):更为一般的进程间通信机制,可用于不同机器之间的进程间通信。起初是由Unix系统的BSD分支开发出来的,但现在一般可以移植到其它类Unix系统上:Linux和System V的变种都支持套接字。

而在java中我们实现多线程间通信则主要采用"共享变量"和"管道流"这两种方法

方法一 通过访问共享变量的方式(注:需要处理同步问题)

方法二 通过管道流

其中方法一有两种实现方法,即

方法一a)通过内部类实现线程的共享变量

代码如下:

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

}

}

/**

* 通过内部类实现线程的共享变量

*

*/

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)通过实现Runnable接口实现线程的共享变量

代码如下:

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 = 0;

public synchronized void run() {

while (true)

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

the index is " + index++);

}

}

/**

* 通过实现Runnable接口实现线程的共享变量

*/

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 = 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) {

/**

* 创建管道输出流

*/

PipedOutputStream pos = new PipedOutputStream();

/**

* 创建管道输入流

*/

PipedInputStream pis = new PipedInputStream();

try {

/**

* 将管道输入流与输出流连接 此过程也可通过重载的构造函数来实现

*/

pos.connect(pis);

} catch (IOException e) {

e.printStackTrace();

}

/**

* 创建生产者线程

*/

Producer p = new Producer(pos);

/**

* 创建消费者线程

*/

Consumer c = new Consumer(pis);

/**

* 启动线程

*/

p.start();

c.start();

}

}

/**

* 生产者线程(与一个管道输入流相关联)

*

*/

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

}

}

}

/**

* 消费者线程(与一个管道输入流相关联)

*

*/

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中通讯和java通讯录的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。