I have several threads. Each thread haves a while(true) loop inside, where I add cycle-by-cycle text. I don't find a good method to change the while(true) loop with a flag, in such a way that I can close the file when I come out from the cycle. I want to do this when I type something for example, or when I press the Eclipse red button.
This is the constructor (Node is a Thread)
public Node(Channel c, int address) {
    my_address=address;
    try {
        writer = new CSVWriter(new FileWriter(my_address + "_id.csv"), ',', ' ' , ' ' ,"\n");
        writer2 = new CSVWriter(new FileWriter(my_address + "_label.csv"), ',', ' ' , ' ' ,"\n");
        String[] entries = "num#state#duration#event#condition#condition result#action1#action2#backoff value".split("#");
        writer.writeNext(entries);
        writer2.writeNext(entries);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
This is the loop in which I modify the file:
while (true) {
        //write id value
        String id_to_split = num+"#"+fsm.current_state.nome+"#"+tempo_minore+"#"+
                fsm.current_transition.e.getId()+"#"+ fsm.current_transition.c.getId()+"#"+
                fsm.current_transition.c.getFlag()+"#"+fsm.current_transition.a.getId()+"#"+
                fsm.current_transition.a2.getId()+"#"+backoff;
        String[] id_entries = id_to_split.split("#");
        writer.writeNext(id_entries);
        //write name
        String label_to_split = num+"#"+fsm.current_state.nome+"#"+tempo_minore+"#"+
                fsm.current_transition.e.getLabel()+"#"+fsm.current_transition.c.getLabel()+"#"+
                fsm.current_transition.c.getFlag()+"#"+fsm.current_transition.a.getLabel()+"#"+
                fsm.current_transition.a2.getLabel()+"#"+backoff;
        String[] label_entries = label_to_split.split("#");
        writer2.writeNext(label_entries);
        num++;
}
    closeCSVs();
}
The method closeCSVs():
    public void closeCSVs() {
    try {
        writer.close();
        writer2.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}