kimos123
asked on
Java thread
Hi,
I want to add a threat to my program but i dont know how to do it:
i have time string in my program and i want the threat to start from 0, and everytime it increase by 1 and look if there is something with the time 1 and it have to do something, and then it goes to 2 and look again and so on...
the problem is that i am parsing/binding my time string from a xml file as follow:
if(e.getSource() == menuItem1){
int returnVal = fc2.showOpenDialog(GUI.thi s);
if (returnVal == JFileChooser.APPROVE_OPTIO N) {
try {
File file2 = fc2.getSelectedFile();
System.out.println("Openin g:"+file2. getName()) ;
binder.xml.Annotations annotation = binder.xml.AnnotationsUnma rshaller.u nmarshal(f ile2, false);
List annotationlist = annotation.getAnnotationLi st();
for (Iterator i = annotationlist.iterator(); i.hasNext(); ) {
binder.xml.Annotation annotation2 = (binder.xml.Annotation)i.n ext();
if(annotation2.getType().e quals("hum an")){
numberofhumans++;
h_id = annotation2.getId();
h_starttime = annotation2.getStarttime() ;
h_endtime = annotation2.getEndtime();
List video = annotation2.getVideo_prope rtiesList( );
for (Iterator j = video.iterator(); j.hasNext(); ) {
binder.xml.Video_propertie s video2 = (binder.xml.Video_properti es)j.next( );
h_is_visible = video2.getIs_visible();
}
List audio = annotation2.getAudio_prope rtiesList( );
for (Iterator j = audio.iterator(); j.hasNext(); ) {
binder.xml.Audio_propertie s audio2 = (binder.xml.Audio_properti es)j.next( );
h_makes_sound = audio2.getMakes_sound();
h_sound_volume = audio2.getSound_volume();
}
}
so if h_starttime = 0 it will contrinue if not it goes to 1 and look for starttime 1 and so on..
ragards,
kimos
I want to add a threat to my program but i dont know how to do it:
i have time string in my program and i want the threat to start from 0, and everytime it increase by 1 and look if there is something with the time 1 and it have to do something, and then it goes to 2 and look again and so on...
the problem is that i am parsing/binding my time string from a xml file as follow:
if(e.getSource() == menuItem1){
int returnVal = fc2.showOpenDialog(GUI.thi
if (returnVal == JFileChooser.APPROVE_OPTIO
try {
File file2 = fc2.getSelectedFile();
System.out.println("Openin
binder.xml.Annotations annotation = binder.xml.AnnotationsUnma
List annotationlist = annotation.getAnnotationLi
for (Iterator i = annotationlist.iterator();
binder.xml.Annotation annotation2 = (binder.xml.Annotation)i.n
if(annotation2.getType().e
numberofhumans++;
h_id = annotation2.getId();
h_starttime = annotation2.getStarttime()
h_endtime = annotation2.getEndtime();
List video = annotation2.getVideo_prope
for (Iterator j = video.iterator(); j.hasNext(); ) {
binder.xml.Video_propertie
h_is_visible = video2.getIs_visible();
}
List audio = annotation2.getAudio_prope
for (Iterator j = audio.iterator(); j.hasNext(); ) {
binder.xml.Audio_propertie
h_makes_sound = audio2.getMakes_sound();
h_sound_volume = audio2.getSound_volume();
}
}
so if h_starttime = 0 it will contrinue if not it goes to 1 and look for starttime 1 and so on..
ragards,
kimos
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
but my code runs inside a loop and i cant define it in a new void.
and i dont see where i can do the time thread +1 sec everytime ?
and i dont see where i can do the time thread +1 sec everytime ?
Well what you could do is use a counter and a Thread.sleep(//time in milliseconds); command inside your loop.
Cheers,
Ricky
Cheers,
Ricky
ASKER
saintsairforce, Thread.sleep(//time in milliseconds); without having to do thos lines:
//in main program
MyThread thread = new MyThread();
thread.start();
class MyThread extends Thread
{
public MyThread ()
{
//define any special parameters for thread
}
public void run()
{
//your code to run in seperate thread
}
}
??
//in main program
MyThread thread = new MyThread();
thread.start();
class MyThread extends Thread
{
public MyThread ()
{
//define any special parameters for thread
}
public void run()
{
//your code to run in seperate thread
}
}
??
remember to have your jdoc decriptor turn on.
you don't want to use some deprecated methods in Threads.. and there are loads of them in there.....
you don't want to use some deprecated methods in Threads.. and there are loads of them in there.....
ASKER
here is what i did, it doesnt work so well, and i get the messgae "Finally" when its not done.
and the main problem now is that my GUI dont get updated until everything is done, and i want the gui to be updated at the same time the tread is alive
private static class MessageLoop implements Runnable {
public void run() {
JessEngine.dorun();
try {
for (int i = 0; i < numberanot; i++) {
//Pause for 1 seconds
Thread.sleep(1000);
}
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
}
static void threadMessage(String message) {
String threadName = Thread.currentThread().get Name();
System.out.format("%s: %s%n", threadName, message);
}
and i placed the following command where the loop is to start the tread:
long patience = 1000 * 60 * 60;
threadMessage("Starting MessageLoop thread");
long startTime = System.currentTimeMillis() ;
Thread t = new Thread(new MessageLoop());
t.start();
threadMessage("Waiting for MessageLoop thread to finish");
//loop until MessageLoop thread exits
while (t.isAlive()) {
threadMessage("Still waiting...");
//Wait maximum of 1 second for MessageLoop thread to
//finish.
t.join(1000);
if (((System.currentTimeMilli s() - startTime) > patience) &&
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
//Shouldn't be long now -- wait indefinitely
t.join();
}
}
threadMessage("Finally!");
so how can i fix it?
and the main problem now is that my GUI dont get updated until everything is done, and i want the gui to be updated at the same time the tread is alive
private static class MessageLoop implements Runnable {
public void run() {
JessEngine.dorun();
try {
for (int i = 0; i < numberanot; i++) {
//Pause for 1 seconds
Thread.sleep(1000);
}
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
}
}
}
static void threadMessage(String message) {
String threadName = Thread.currentThread().get
System.out.format("%s: %s%n", threadName, message);
}
and i placed the following command where the loop is to start the tread:
long patience = 1000 * 60 * 60;
threadMessage("Starting MessageLoop thread");
long startTime = System.currentTimeMillis()
Thread t = new Thread(new MessageLoop());
t.start();
threadMessage("Waiting for MessageLoop thread to finish");
//loop until MessageLoop thread exits
while (t.isAlive()) {
threadMessage("Still waiting...");
//Wait maximum of 1 second for MessageLoop thread to
//finish.
t.join(1000);
if (((System.currentTimeMilli
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
//Shouldn't be long now -- wait indefinitely
t.join();
}
}
threadMessage("Finally!");
so how can i fix it?
ASKER
or maybe, just tell me how to make a timer or thread that start from, 0 and goes to 5000 sec, and check everyt second if my object starttime is equal to the timer time at that moment if so, then it execute a code, if there is no then the time goes to +1 second and again it check if there is starttime that is qual to the timer then it do some stuff and soo on.
and also to make sure that my GUI get updated live with the timer.
regards
kimos
and also to make sure that my GUI get updated live with the timer.
regards
kimos
Sorry, didn't read your last msg... too busy... work system crashed...
afee 4 the next few days
afee 4 the next few days
ASKER
its ok, i used a timer
new MyThread().start();
I assume that your thread is called MyThread.