实现线程之间通信的方法 java间的线程通信
java间的线程通信
不同线程间进行通信通常有两种简单方法
方法一 通过访问共享变量的方式(注 需要处理同步问题)
方法二 通过管道流
其中方法一有两种实现方法 即
方法一a)通过内部类实现线程的共享变量
代码如下
Java代码
/**
* 通过内部类实现线程的共享变量
*
*/
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 = ;
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 = ;
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接口实现线程的共享变量
代码如下
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()
}
}
class Mythread implements Runnable {
int index = ;
public synchronized void run() {
while (true)
System out println(Thread currentThread() 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()
}
}
class Mythread implements Runnable {
int index = ;
public synchronized void run() {
while (true)
System out println(Thread currentThread() getName()
+ is running and the index is + index++)
}
}
方法二
代码如下
Java代码
import java io IOException;
import java io PipedInputStream;
import java io PipedOutputStream;
public class CommunicateWhitPiping {
public static void main(String[] args) {
/**
* 创建管道输出流
*/
PipedOutputStream pos = new PipedOutputStream()
/**
* 创建管道输入流
*/
PipedInputStream pis = new PipedInputStream()
try {
/**
* 将管道输入流与输出流连接
* 此过程也可通过重载的构造函数来实现
*/
nnect(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 = ;
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()
}
}
lishixinzhi/Article/program/Java/gj/201311/27464相关文章
- 疯狂Java讲义:使用Socket进行通信[1]
- 高并发socket服务 Java进行并发多连接socket编程
- Java网络编程从入门到精通(13):使用Socket类接收和发送数据
- java编写简单的通讯录 在java里用最简单的方法编写一个程式,判断输入的是大写字母还是小写字母还是数字,或字串?
- java多线程和线程池 C# 实现的多线程异步Socket数据包接收器框架
- ()奥秘 Java线程通信源代码中的奥秘探究
- java是网络编程语言吗 基于Socket的Java网络编程集粹
- 串行端口是什么意思 浅谈Java串行端口技术协议
- java两个线程共享一个变量 Java多线程共享数据、同步、通信
- java线程通信 java线程简介(线程的生命)
爱学记

微信收款码
支付宝收款码