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

Java automatic search access connectivity libraries
PlaceAutoCompleteFragment unable to start activity java.lang.NullPointerException
Java : ByteBuffer allocateDirect Allocate direct allocate more than demanding
Flat-Mapping Collector for property of a Class using groupingBy
Struggling with getting data from another activity
Can't find class when comparing it's functions in another file
sbt multi-project build dependency
How to resize a LWJGL Window without distorting rendered objects
How can I translate a string without using the strings.xml resource file?
Circle won't move position in JavaFX
IncompatibleClassChangeError … was expected to be of type direct but instead was found to be of type virtual
Reload WebView in Java
How to use param object instead of several params when there are too many optional parameters for a REST endpoint
Why does Sprite.draw does not render my sprite, but Batch.render and SpriteBatch.render do?
Why doesn't Dijkstra's algorithm work as it should for given graph?
How do I call a method in a adapter who is initialized in a fragment that is contained in an Activity?

Categories

HOME
osgi
layout
cvs
programming-languages
jsrender
webpack-2
retrofit
flyway4
onelogin
cloudkit
facebook-php-sdk
nstableview
windows-azure-storage
task
percona
try-catch
quartz-scheduler
django-admin
undefined
ab-testing
cloudhub
excel-2007
zurb-foundation-6
dxf
underflow
facet
url-scheme
elasticsearch-net
subdomains
stringtemplate
avcapturesession
android-kernel
lto
gesture
abstract-class
az-application-insights
splice
squib
graphenedb
android-tabhost
g-code
hibernate-tools
dropbox-php
lowpass-filter
knockout-components
dtexec
imanage
hittest
objective-c-swift-bridge
probability-density
elements
merge-conflict-resolution
lua-5.1
blackberry-10
actionbardrawertoggle
dataview
pagerank
nessus
php-parse-error
revolution-r
pyke
cloudbees
photobucket
jms2
ruby-2.2
kineticjs
tween
xc16
issuu
onactivityresult
zend-route
article
reactfx
mysql-error-1062
imdbpy
aqtime
back-stack
delphi-6
unc
google-email-migration
twrequest
regsvr32
uiviewanimation-curve
windows-phone-7.1.1
hgsubversion
nsobject
heartbeat
joyent
webkit.net
kdbg
mtj
multiple-languages
3gp
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App