Note that there are some explanatory texts on larger screens.

plurals
  1. POOutputStream doesn't work with Threads
    primarykey
    data
    text
    <p>Could you possible help me solving the next problem? I'm trying to use PrintStream with my Threads to make them writing something to different outputs. The problem is: System.out works perfect, but when I try to add </p> <pre><code>File f1 = new File("src/task7/simple/1.txt"); PrintStream filePRinPrintStream = new PrintStream(f1) </code></pre> <p>my Thread doesn't want to write anything, it throws no warning or exception while ordinary printing is main() method is OK. </p> <pre><code>filePRinPrintStream.println("PREVED"); </code></pre> <p>Here are my code extracts for your information. </p> <pre><code>public class NamePrinterThread implements Runnable, NamePrinterIF{ private String name; private PrintStream outputStream; private long interval; private int count; @Override public void setPrintName(String name) { this.name = name; } @Override public void setStream(PrintStream stream) { if(stream == null) throw new NullPointerException("PrintStream is null"); this.outputStream = stream; } @Override public void setInterval(long ms) { if(ms &lt;= 0) throw new IllegalArgumentException("Printing interval can't be negative"); this.interval = ms; } @Override public void setCount(int count) { if(count &lt;= 0) throw new IllegalArgumentException("Printing count can't be negative"); this.count = count; } @Override public void run() { for (int i = 0; i &lt; this.count; i++) { try { outputStream.println(this.name); Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } } } public void startThread(){ new Thread(this, name).start(); } } </code></pre> <p>and main method from another class</p> <pre><code>public static void main(String[] args) { File f1 = new File("src/task7/simple/1.txt"); try(PrintStream filePRinPrintStream = new PrintStream(f1)){ //filePRinPrintStream.println("PREVED"); NamePrinterIF thread2 = new NamePrinterThread(); thread2.setCount(20); thread2.setInterval(350); thread2.setStream(filePRinPrintStream); thread2.setPrintName("thread2"); thread2.startThread(); filePRinPrintStream.flush(); } catch(IOException e){ e.printStackTrace(); } NamePrinterIF thread1 = new NamePrinterThread(); thread1.setCount(10); thread1.setInterval(200); thread1.setStream(System.out); thread1.setPrintName("thread1"); thread1.startThread(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload