java


how can I stop the execution of the while loop with a keyboard input?


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();
}
}
If I understood your question correctly, what you're looking for is either a try-with-resources block which works like following:
try(FileReader reader = new FileReader("path")) {
while(true) {
//use resources
}
}
You can use this with any Class that implements the AutoClosable-Interface (basically every class that offers a .close()-Method).
The resource will be closed automatically after the try-block is escaped.
Same solution different code would be to wrap with a classic try and adding a finally block to it.
try {
FileReader reader = new FileReader("path");
while(true) {
//use resources
}
} finally {
reader.close();
}
You may need to implement closing file in shutdown hook Runtime.addShutdownHook

Related Links

Under what circumstances could closing a stream fail? [duplicate]
restrict jtextfield to accept only numbers? [duplicate]
BMR Calculator testing
Java Error - array required, but java.lang.String found
How to add elements to a LinkedList that is contained within a HashMap?
Why singletons static method getNext() returns incremented value
How to find top level not annotated classes in IntelliJ IDEA
OneToOne with Composite Key
How to short or group data according to data value in xslt
iTextPDF - Unable to use getOverContent() as a paremeter for table.writeSelectedRows() when internal anchors have been added to the table
How can everything be an object? [closed]
Context initialization failed Error with Spring MVC
How to redirect to another page using jsoup and continue to print content in ListView?
Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task :app:compileDebugJavaWithJavac
How to detect mouse moving outside Java Popup?
Why is output not showing the Prime Numbers on the Display?

Categories

HOME
ngrx
push-notification
elm
jxls
umd
echarts
webrequest
python-unittest
google-translate
adobe-analytics
mapserver
quickbooks
nstableview
hex-editors
fallback
decomposition
caml
systemc
beyondcompare
custom-wordpress-pages
object-detection
riot.js
cloudhub
wkwebview
icloud-api
vlsi
user-interaction
react-css-modules
maquette
atlassian-crucible
webkitspeechrecognition
windowbuilder
kendo-datasource
srcset
skia
http-status-code-503
airconsole
vsts-build-task
xcode-extension
dynamics-crm-2013
sharefile
impersonation
jna
graphenedb
sas-visual-analytics
sfdc
http-live-streaming
nand2tetris
arena-simulation
no-www
.net-4.6.2
celery-task
sage-one
svmlight
diagnostics
gcsfuse
imanage
mcafee
orthogonal
chord-diagram
pycaffe
angular-strap
etsy
fputcsv
react-native-listview
qcustomplot
python-stackless
jqgrid-formatter
clipperlib
c3
websocket4net
preferences
eol
p4java
imdbpy
quantlib-swig
cos
seed
plasma
simba
libstdc++
amazon-appstore
stage
disclosure
celltable
pyinotify
sql-server-profiler
datareader
gallio
dmx512
gamma
revisions
nhibernate.search
asp.net-profiles
nerddinner
mdac

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App