当前位置: 技术问答>java相关
JAVA初学者的小问题,各位大侠帮帮忙。
来源: 互联网 发布时间:2015-06-07
本文导语: 请问类和类之间如何通讯?线程和线程之间又如何通讯? Java有象VC里面的消息的东西吗? 先谢谢大家了。 | 我只对管道通信蛮清楚的说 core in java里面有个很不错的管道通信例子 程序如下 import java.util...
请问类和类之间如何通讯?线程和线程之间又如何通讯?
Java有象VC里面的消息的东西吗?
先谢谢大家了。
Java有象VC里面的消息的东西吗?
先谢谢大家了。
|
我只对管道通信蛮清楚的说
core in java里面有个很不错的管道通信例子
程序如下
import java.util.*;
import java.io.*;
public class PipeTest
{ public static void main(String args[])
{ try
{ /* set up pipes */
PipedOutputStream pout1 = new PipedOutputStream();
PipedInputStream pin1 = new PipedInputStream(pout1);
PipedOutputStream pout2 = new PipedOutputStream();
PipedInputStream pin2 = new PipedInputStream(pout2);
/* construct threads */
Producer prod = new Producer(pout1);
Filter filt = new Filter(pin1, pout2);
Consumer cons = new Consumer(pin2);
/* start threads */
prod.start();
filt.start();
cons.start();
}
catch (IOException e){}
}
}
class Producer extends Thread
{ public Producer(OutputStream os)
{ out = new DataOutputStream(os);
}
public void run()
{ while (true)
{ try
{ double num = rand.nextDouble();
out.writeDouble(num);
out.flush();
sleep(Math.abs(rand.nextInt() % 1000));
}
catch(Exception e)
{ System.out.println("Error: " + e);
}
}
}
private DataOutputStream out;
private Random rand = new Random();
}
class Filter extends Thread
{ public Filter(InputStream is, OutputStream os)
{ in = new DataInputStream(is);
out = new DataOutputStream(os);
}
public void run()
{ for (;;)
{ try
{ double x = in.readDouble();
total += x;
count++;
if (count != 0) out.writeDouble(total / count);
}
catch(IOException e)
{ System.out.println("Error: " + e);
}
}
}
private DataInputStream in;
private DataOutputStream out;
private double total = 0;
private int count = 0;
}
class Consumer extends Thread
{ public Consumer(InputStream is)
{ in = new DataInputStream(is);
}
public void run()
{ for(;;)
{ try
{ double avg = in.readDouble();
if (Math.abs(avg - old_avg) > 0.01)
{ System.out.println("Current average is " + avg);
old_avg = avg;
}
}
catch(IOException e)
{ System.out.println("Error: " + e);
}
}
}
private double old_avg = 0;
private DataInputStream in;
}
core in java里面有个很不错的管道通信例子
程序如下
import java.util.*;
import java.io.*;
public class PipeTest
{ public static void main(String args[])
{ try
{ /* set up pipes */
PipedOutputStream pout1 = new PipedOutputStream();
PipedInputStream pin1 = new PipedInputStream(pout1);
PipedOutputStream pout2 = new PipedOutputStream();
PipedInputStream pin2 = new PipedInputStream(pout2);
/* construct threads */
Producer prod = new Producer(pout1);
Filter filt = new Filter(pin1, pout2);
Consumer cons = new Consumer(pin2);
/* start threads */
prod.start();
filt.start();
cons.start();
}
catch (IOException e){}
}
}
class Producer extends Thread
{ public Producer(OutputStream os)
{ out = new DataOutputStream(os);
}
public void run()
{ while (true)
{ try
{ double num = rand.nextDouble();
out.writeDouble(num);
out.flush();
sleep(Math.abs(rand.nextInt() % 1000));
}
catch(Exception e)
{ System.out.println("Error: " + e);
}
}
}
private DataOutputStream out;
private Random rand = new Random();
}
class Filter extends Thread
{ public Filter(InputStream is, OutputStream os)
{ in = new DataInputStream(is);
out = new DataOutputStream(os);
}
public void run()
{ for (;;)
{ try
{ double x = in.readDouble();
total += x;
count++;
if (count != 0) out.writeDouble(total / count);
}
catch(IOException e)
{ System.out.println("Error: " + e);
}
}
}
private DataInputStream in;
private DataOutputStream out;
private double total = 0;
private int count = 0;
}
class Consumer extends Thread
{ public Consumer(InputStream is)
{ in = new DataInputStream(is);
}
public void run()
{ for(;;)
{ try
{ double avg = in.readDouble();
if (Math.abs(avg - old_avg) > 0.01)
{ System.out.println("Current average is " + avg);
old_avg = avg;
}
}
catch(IOException e)
{ System.out.println("Error: " + e);
}
}
}
private double old_avg = 0;
private DataInputStream in;
}
|
管道和,互斥信号量是两种线程通信的机制。
谁知道的继续补充。
谁知道的继续补充。