import java.io.IOException;
import java.io.PipedOutputStream;
public class Producer implements Runnable {
private PipedOutputStream pipedOutputStream;
public Producer() {
pipedOutputStream = new PipedOutputStream();
}
public PipedOutputStream getPipedOutputStream() {
return pipedOutputStream;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
pipedOutputStream.write(("This is a test, Id=" + i + "!\n").getBytes());
}
pipedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Consumer
import java.io.IOException;
import java.io.PipedInputStream;
public class Consumer implements Runnable {
private PipedInputStream pipedInputStream;
public Consumer() {
pipedInputStream = new PipedInputStream();
}
public PipedInputStream getPipedInputStream() {
return pipedInputStream;
}
@Override
public void run() {
int len = -1;
byte[] buffer = new byte[1024];
try {
while ((len = pipedInputStream.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
pipedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
主函数
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Producer p = new Producer();
Consumer c = new Consumer();
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
try {
p.getPipedOutputStream().connect(c.getPipedInputStream());
t2.start();
t1.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. PipedReader / PipedWriter (操作字符流)
Producer
import java.io.IOException;
import java.io.PipedWriter;
public class Producer implements Runnable {
private PipedWriter pipedWriter;
public Producer() {
pipedWriter = new PipedWriter();
}
public PipedWriter getPipedWriter() {
return pipedWriter;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
pipedWriter.write("This is a test, Id=" + i + "!\n");
}
pipedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Consumer
import java.io.IOException;
import java.io.PipedReader;
public class Consumer implements Runnable {
private PipedReader pipedReader;
public Consumer() {
pipedReader = new PipedReader();
}
public PipedReader getPipedReader() {
return pipedReader;
}
@Override
public void run() {
int len = -1;
char[] buffer = new char[1024];
try {
while ((len = pipedReader.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
pipedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
主函数
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Producer p = new Producer();
Consumer c = new Consumer();
Thread t1 = new Thread(p);
Thread t2 = new Thread(c);
try {
p.getPipedWriter().connect(c.getPipedReader());
t2.start();
t1.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
想查看上面几种方式的完整代码,请点击这里:生产者消费者问题的一些实验
参考
《Java多线程编程核心技术》 高洪岩 生产者/消费者问题的多种Java实现方式 Producer–consumer problem – Wikipedia Semaphore的一种使用方法 Semaphore实现的生产者消费者程序
利用JavaScript进行对象排序,根据用户的年龄排序展示
<script>
var bob={
name;bob,
age:30
}
var peter={
name;peter,
age:30
}
var amy={
name;amy,
age:24
}
var mike={
name;mike,
age:29
}
var john={
FLP
One famous theory in distributed computing, known as FLP after the authors Fischer, Lynch, and Patterson, proved that in a distributed system with asynchronous communication and process crashes,
每一行命令都是用分号(;)作为结束
对于MySQL,第一件你必须牢记的是它的每一行命令都是用分号(;)作为结束的,但当一行MySQL被插入在PHP代码中时,最好把后面的分号省略掉,例如:
mysql_query("INSERT INTO tablename(first_name,last_name)VALUES('$first_name',$last_name')");
题目链接:zoj 3820 Building Fire Stations
题目大意:给定一棵树,选取两个建立加油站,问说所有点距离加油站距离的最大值的最小值是多少,并且任意输出一种建立加油站的方式。
解题思路:二分距离判断,判断函数的复杂度是o(n),这样的复杂度应该是o(nlogn),即使常数系数偏大,但是居然跑了4.5s,也是醉了。 判断函数里面做了3次bfs,但是每次bfs节点最多