Link to home
Start Free TrialLog in
Avatar of kimos123
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.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
               
                try {
                    File file2 = fc2.getSelectedFile();
                    System.out.println("Opening:"+file2.getName());
                    binder.xml.Annotations annotation = binder.xml.AnnotationsUnmarshaller.unmarshal(file2, false);
                   
                    List annotationlist = annotation.getAnnotationList();
                    for (Iterator i = annotationlist.iterator(); i.hasNext(); ) {
                        binder.xml.Annotation annotation2 = (binder.xml.Annotation)i.next();
                       
                       
                        if(annotation2.getType().equals("human")){
                            numberofhumans++;
                                             
                            h_id = annotation2.getId();
                            h_starttime = annotation2.getStarttime();
                            h_endtime = annotation2.getEndtime();
                           
                            List video = annotation2.getVideo_propertiesList();
                            for (Iterator j = video.iterator(); j.hasNext(); ) {
                                binder.xml.Video_properties video2 = (binder.xml.Video_properties)j.next();
                               
                                h_is_visible = video2.getIs_visible();
                            }
                           
                            List audio = annotation2.getAudio_propertiesList();
                            for (Iterator j = audio.iterator(); j.hasNext(); ) {
                                binder.xml.Audio_properties audio2 = (binder.xml.Audio_properties)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
Avatar of girionis
girionis
Flag of Greece image

I am not sure what you are trying to do here. But in order to start a new Thread you should do something like

new MyThread().start();

I assume that your thread is called MyThread.
ASKER CERTIFIED SOLUTION
Avatar of SamsonChung
SamsonChung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kimos123
kimos123

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 ?
Well what you could do is use a counter and a Thread.sleep(//time in milliseconds); command inside your loop.

Cheers,
Ricky
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
       }
  }

??
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.....

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().getName();
        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.currentTimeMillis() - 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?
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
Sorry, didn't read your last msg... too busy... work system crashed...

afee 4 the next few days
its ok, i used a timer