Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Java Concurrency

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
NotReallySureWhatGoesHere
Expert Cheater
Reputation: -1

Joined: 20 Feb 2012
Posts: 110

PostPosted: Thu Mar 08, 2012 7:52 am    Post subject: Java Concurrency Reply with quote

I don't know much (if any at all) about it and am in need of it (I think). For this project I am doing I don't need to know of it in great detail, only in enough to do one thing; update a label's text.

Alright so:
Code:
private void updateTime() {
        synchronized (InterfaceDialog.class) {
            lblTime.setText(String.valueOf(time));
        }
    }


I'm trying to get this method to run at the same time with the main thread so it can continuously update - then again that may not even be what I need, I honestly have no idea.

Here is my main function (please note the IDE auto-added the invokeLater() content):
Code:
public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                InterfaceDialog form = new InterfaceDialog();
                form.setLocationRelativeTo(null);
                form.setVisible(true);
            }
        });
    }


I have a StopWatch class (trying to be more OOP as suggested) and I'm not not sure how I can call a method in that class to update a label on the "main" class ??? Anyway, I did some Google'ing and that's what I came up with. I have called the updateTime() method and I get no update Embarassed Suggestions (no code please - or very little as possible) as you guys provided me last topic would be very helpful. Thank you!
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Mar 08, 2012 8:29 am    Post subject: Reply with quote

You only need concurrency if you have 2+ distinct threads running where one thread is trying to read while another is trying to write. I've only really had to use it in the case of ArrayList's where information was being deleted/added and fucking up looking at the data in another thread.

If you are trying to update the label from an external class, you could pass the instance of the label into the StopWatch class in the constructor and use it to directly modify the label. Or you could use a singleton pattern on your main instance and directly get the current instance of the class containing the label.

Also, maybe it's just how I code but that above code looks very weird o.O I typically use Eclipse and not netbeans though.

_________________
Back to top
View user's profile Send private message
NotReallySureWhatGoesHere
Expert Cheater
Reputation: -1

Joined: 20 Feb 2012
Posts: 110

PostPosted: Fri Mar 09, 2012 8:00 am    Post subject: Reply with quote

Thanks for the input and suggestions, however I'm not sure how to do either so I just used a Timer Razz

Anyway here is some of the code:
Code:
timerTask = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                updateTime();
                lblTime.setText(String.valueOf(time));
            }
        };

        t = new Timer(StaticInfo.TIMER_DELAY, timerTask);
        t.start();


Seems to work fine. Thanks again though.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Fri Mar 09, 2012 12:13 pm    Post subject: Reply with quote

if I ever make timers I just do a simple thread

Code:
class TimeKeeper extends Thread
{
   boolean stop = false;
   public TimeKeeper(...) { anything you need to pass in here }
   public void run()
   {
        while(!stop)
        {
            try { Thread.sleep(durationMS); } catch(Exception e) {}
        }
   }
   public boolean endThread() { stop = true; }
}

_________________
Back to top
View user's profile Send private message
NotReallySureWhatGoesHere
Expert Cheater
Reputation: -1

Joined: 20 Feb 2012
Posts: 110

PostPosted: Sat Mar 10, 2012 12:18 pm    Post subject: Reply with quote

HomerSexual wrote:
if I ever make timers I just do a simple thread

Code:
class TimeKeeper extends Thread
{
   boolean stop = false;
   public TimeKeeper(...) { anything you need to pass in here }
   public void run()
   {
        while(!stop)
        {
            try { Thread.sleep(durationMS); } catch(Exception e) {}
        }
   }
   public boolean endThread() { stop = true; }
}


Ah, that's a good idea too!

I have a question though: would the extending Thread be another thread or would it remain on the same thread as my main application? Embarassed Because it would probably be better to have 2 different threads (?), however the the form doesn't ever seem to freeze or stop-working while the Timer is running and the text is being changed. Thanks again!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites