不同线程间进行通信通常有两种简单方法:
方法一 通过访问共享变量的方式(注:需要处理同步问题)
方法二 通过管道流
其中方法一有两种实现方法,即
方法一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();
}
}
方法二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();
- }
- }
-
-
-
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++);
}
}
方法二:
代码如下
- 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();
- }
- }
- }
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();
}
}
}
分享到:
相关推荐
Qt线程之间通信、线程和进程之间通信实例,简单易懂
1.方式1:继承 QThread 类方式:...两种方式的选择取决于具体的需求和设计。仅需简单的线程操作,继承 QThread 类方式可能更为简洁。如果需要更灵活的线程控制和对象交互,使用类对象的 moveToThread() 方式可能更适合
Java是一种可用于进行网络编程的语言,它提供了两种功能强大的网络支持机制:URL访问网络资源的类和用Socket通讯的类,来满足不同的要求。一是URL用于访问Internet网上资源的应用;另一种是针对client/server(客户端...
比如,最简单的情况是,一个计数器,两个线程 同时加一,后果就是损失了一个计数,但相当频繁的锁定又可能带来性能上的消耗,还有最可怕的情况死锁。那么什么情况下我们需要使用锁,什么情况下不需要 呢? 1)只有...
【作业】:编写程序,实现上述两种死锁现象。 读写锁 与互斥量类似,但读写锁允许更高的并行性。其特性为:写独占,读共享。 读写锁状态: 一把读写锁具备三种状态: 1. 读模式下加锁状态 (读锁) 2. ...
TCP/IP通信协议是一种可靠的网络协议,它在通信的两端各建立一个Socket,从而在通信的两端之间形成网络虚拟链路。一旦建立了虚拟的网络链路,两端的程序就可以通过虚拟链路进行通信了。Java对基于TCP协议的网络通信...
在Win32下,可以使用两种编程方式实现串口通信,其一是使用ActiveX控件,这种方法程序简单,但欠灵活。其二是调用Windows的API函数,这种方法可以清楚地掌握串口通信的机制,并且自由灵活。本文我们只介绍API串口...
在不同的系统中thread_t这个类型有不同的实现,它可能是一个整数值,也可能是一个结构体,也可能是一个地址,所以不能简单地当成整数用printf打印,调用pthread_self(3)可以获得当前线程的id。 attr参数表示线程属性...
本文通过一些具体的例子简单介绍一下python的多线程和多进程,后续会写一些进程通信和线程通信的一些文章。 python多线程 python中提供两个标准库thread和threading用于对线程的支持,python3中已放弃对前者的支持...
本文通过一些具体的例子简单介绍一下python的多线程和多进程,后续会写一些进程通信和线程通信的一些文章。 python中提供两个标准库thread和threading用于对线程的支持,python3中已放弃对前者的支持,后者是一种更...
在Win32下,可以使用两种编程方式实现串口通信,其一是使用ActiveX控件,这种方法程序 Windows的API函数,这种方法可以清楚地掌握串口通信的机制,并且自由灵活。本文我们只介绍 串口的操作可以有两种操作方式:同步...
实际是接收联通短信中心SMG发过来的信息,为了应对高并发的场景,使用了生产者和消费者典型的多线程设计方案,用线程池去管理多线程,需要改动相关代码,找到类SCIPServer修改即可,接收短信的方式有两种一种是Socket,一种...
237、触发器分为事前触发和事后触发,这两种触发有和区别。语句级触发和行级触发有何区别。 56 238、EJB容器提供的服务 56 239、EJB的角色和三个对象 56 240、EJB的几种类型 56 241、bean 实例的生命周期 56 242、...
在之前的博文中简单介绍过如何实现fragment之间的信息交互:《Android中Fragment与Activity之间的交互(两种实现方式)》,今天继续给大家介绍一种可以实现此效果的另外一种方式EventBus。(相比于handler,接口回调...
237、触发器分为事前触发和事后触发,这两种触发有和区别。语句级触发和行级触发有何区别。 56 238、EJB容器提供的服务 56 239、EJB的角色和三个对象 56 240、EJB的几种类型 56 241、bean 实例的生命周期 56 242、...
《Linux多线程服务端编程:使用muduo C++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。...
Session Bean 还可以再细分为 Stateful Session Bean 与 Stateless Session Bean ,这两种的 Session Bean都可以将系统逻辑放在 method之中执行,不同的是 Stateful Session Bean 可以记录呼叫者的状态,因此通常来...
Session Bean 还可以再细分为 Stateful Session Bean 与 Stateless Session Bean ,这两种的 Session Bean都可以将系统逻辑放在 method之中执行,不同的是 Stateful Session Bean 可以记录呼叫者的状态,因此通常...
和长流分段两种方法,从不同角度优化CPU.GPU的通信过程。其次,针对AMD GPU硬件及其编程模型Brook+的特性,本文提出了四种面向GPU的并行优化策 略,包括平衡线程并行性和局部性、分支消除、开发指令级并行和提高...
electron打印大概有两种: 第一种:通过window的webcontent对象,使用此种方式需要单独开出一个打印的窗口,可以将该窗口隐藏,但是通信调用相对复杂。 第二种:使用页面的webview元素调用打印,可以将webview隐藏...