方法一:通过访问共享变量方式(需处理同步问题)
方法一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接口实现线程的共享变量
代码如下
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++);
}
}
方法二: 通过管道流
问题:只能实现1对1的生产消费。
代码如下
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();
}
/**
* 创建生产者线程
*/
ProducerPipe p = new ProducerPipe(pos);
/**
* 创建消费者线程
*/
ConsumerPipe c1 = new ConsumerPipe(pis);
ConsumerPipe c2 = new ConsumerPipe(pis);
/**
* 启动线程
*/
p.start();
c1.start();
c2.start();
}
}
/**
* 生产者线程(与一个管道输入流相关联)
*
*/
class ProducerPipe extends Thread {
private PipedOutputStream pos;
public ProducerPipe(PipedOutputStream pos) {
this.pos = pos;
}
public void run() {
int i = 8;
try {
System.out.println(Thread.currentThread().getName() + " sending i " );
pos.write(i);
System.out.println(Thread.currentThread().getName() + " sent i " );
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 消费者线程(与一个管道输入流相关联)
*
*/
class ConsumerPipe extends Thread {
private PipedInputStream pis;
public ConsumerPipe(PipedInputStream pis) {
this.pis = pis;
}
public void run() {
try {
System.out.println(Thread.currentThread().getName() + " reading i " );
System.out.println(pis.read());
System.out.println(Thread.currentThread().getName() + " read i " );
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法三: Wait/Notify结合共享变量
问题:
代码如下
class ShareData
{
private char c;
private boolean isProduced = false; // 信号量
public synchronized void putShareChar(char c) // 同步方法putShareChar()
{
if (isProduced) // 如果产品还未消费,则生产者等待
{
try
{
wait(); // 生产者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.c = c;
isProduced = true; // 标记已经生产
notify(); // 通知消费者已经生产,可以消费
}
public synchronized char getShareChar() // 同步方法getShareChar()
{
if (!isProduced) // 如果产品还未生产,则消费者等待
{
try
{
wait(); // 消费者等待
} catch (InterruptedException e) {
e.printStackTrace();
}
}
isProduced = false; // 标记已经消费
notify(); // 通知需要生产
return this.c;
}
}
class Producer extends Thread // 生产者线程
{
private ShareData s;
Producer(ShareData s)
{
this.s = s;
}
public void run()
{
for (char ch = 'A'; ch <= 'D'; ch++)
{
try
{
Thread.sleep((int) (1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
s.putShareChar(ch); // 将产品放入仓库
System.out.println(ch + " is produced by Producer in "+Thread.currentThread().getName());
}
}
}
class Consumer extends Thread // 消费者线程
{
private ShareData s;
Consumer(ShareData s)
{
this.s = s;
}
public void run()
{
char ch;
do {
/*
try
{
Thread.sleep((int) (Math.random() * 3000));
} catch (InterruptedException e) {
e.printStackTrace();
}
*/
ch = s.getShareChar(); // 从仓库中取出产品
System.out.println(ch + " is consumed by Consumer in "+Thread.currentThread().getName());
} while (ch != 'D');
}
}
class CommunicationDemo
{
public static void main(String[] args)
{
ShareData s = new ShareData();
//new Producer(s).start();
new Producer(s).start();
new Consumer(s).start();
new Consumer(s).start();
}
}