java线程通信
2009-02-03 09:26
183 查看
不同线程间进行通信通常有两种简单方法:
方法一 通过访问共享变量的方式(注:需要处理同步问题)
方法二 通过管道流
其中方法一有两种实现方法,即
方法一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 = 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接口实现线程的共享变量
代码如下
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 = 0;
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 {
/**
* 将管道输入流与输出流连接
* 此过程也可通过重载的构造函数来实现
*/
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();
}
}
}
方法一 通过访问共享变量的方式(注:需要处理同步问题)
方法二 通过管道流
其中方法一有两种实现方法,即
方法一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 = 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接口实现线程的共享变量
代码如下
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 = 0;
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(); } } /* 实现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++); } }
方法二:
代码如下
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 {
/**
* 将管道输入流与输出流连接
* 此过程也可通过重载的构造函数来实现
*/
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线程间通信的几种方式
- Java笔记3 多线程<2>线程间通信-代码分析以及多线程常见方法的运用
- 第23章 java线程通信——生产者/消费者模型案例
- JAVA多线程之线程间的通信方式
- Java多线程与并发应用-(3)-传统线程通信技术及生产者消费者模式
- Java线程(九):Condition-线程通信更高效的方式
- Java线程(九):Condition-线程通信更高效的方式
- Java多线程通信-利用传统的线程通信wait(),notify()方法实现“生产者消费者模式”
- Java并发——使用Condition线程间通信
- Java的多线程-线程间的通信(3)
- Java多线程编程-(4)-线程间通信机制的介绍与使用
- 深入解析Java的线程同步以及线程间通信
- ZeroMQ(java)之I/O线程的实现与组件间的通信
- java 线程间的通信 pipedOutStream 和PipedInputStream 管道流
- java多线程 - 线程通信
- java基础12:线程间通信----等待唤醒机制
- java学习之路——线程3(线程之间的通信)
- JAVA多线程之线程间的通信方式
- Java多线程:线程间通信之volatile与sychronized