我有一个线程使用一些PrintWriter的如何使用进程打开线程?
class NThread implements Runnable {
Thread t;
PrintWriter w;
private volatile boolean running = true;
public NThread(int p, PrintWriter w) {
t = new Thread(this);
this.w = w;
t.setPriority(p);
}
public void run() {
while (running) {
w.println("Id: " + t.getId());
}
}
public void stop() {
running = false;
}
public void start() {
t.start();
}
}
在文件中写的,我尝试使用新的进程中打开这个线程。
public class FileWrite {
public static void main(String[] args) {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
Thread ref_mainThread=Thread.currentThread();
try {
PrintWriter printWriter = new PrintWriter("C:\\write.txt");
printWriter.println("Id main thread" + ref_mainThread);
NThread p1=new NThread(Thread.NORM_PRIORITY + 4,printWriter);
Process process1 = new ProcessBuilder("p1",".start()").start();
try {
Thread.sleep(1000);
}
process1.destroy();
printWriter.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
我尝试通过两个进程写入一个文件。我如何做到这一点?
在编写程序之前,必须阅读java-doc。 – 2014-10-30 14:19:02