[Java] Wait?

AgentPaper

From the depths, I come.
Reaction score
107
I'm having some trouble making my program wait. Basically, at some point I want the program to display an image, an animated .gif file, and then I want it to replace that .gif with another image after the .gif has finished playing after 2 seconds. However, wait(2000) and Thread.sleep(2000) just freeze up the program for 2 seconds, other code doesn't run and the animation doesn't play. So, how can I make my program do something after X seconds, without stopping the rest of the program from running?
 

AgentPaper

From the depths, I come.
Reaction score
107
Hmm, so basically I need to make a new thread, tell that thread to wait for X seconds, and then have that thread do what I want to happen after those X seconds? That seems like a rather clunky way to go about it. Or am I missing something?
 

Vestras

Retired
Reaction score
248
Well in C++ it's System::Threading::Thread::Sleep(100), maybe it's like that too in Java?
 

Xienoph

You can change this now in User CP.
Reaction score
43
Hmm, so basically I need to make a new thread, tell that thread to wait for X seconds, and then have that thread do what I want to happen after those X seconds? That seems like a rather clunky way to go about it. Or am I missing something?

Yes, that's the way to do it. Concurrency like this isn't trivial to do. There are a bunch of problems by making a program multithreaded like that. In fact, I'm sure that's responsible for 90% of bugs out there in the field. Read about Java's synchronize or, more generally, mutexes.

Sorry for flooding you with information. There are a lot of materials in this topic. Hope that helps.
 

AgentPaper

From the depths, I come.
Reaction score
107
Hmm, well I got it working, at any rate. One more question, though, and one I've also been trying to figure out. (and mostly working around so far) Can you pass a method along as an argument/variable to another method, and then have that method later be run by that other code?

What I have now:

Method X calls method delayImageChange(time, img, imgPosition)
method delayImageChange sleeps for X seconds
method delayImageChange does what I want it to do

This works for now, but if I want to add in a wait for any other type of action, I need to add another method to the Wait class to do that. If I could pass the method I want executed after X seconds to a method in the Wait class, however, then I could use the same method for any type of wait I want, without having to add code just for that wait.
 

Xienoph

You can change this now in User CP.
Reaction score
43
No. That's one of the reasons why I don't like Java.

In Java, everything is done by objects. So you have to declare a class that holds the method that you want to pass along. Instantiate the object of that class, and pass that object along.

Yes, it's painful. But that's why there's Groovy.

Actually, I just remembered that there's an easy way to do things after some delay. Look up for Timer and TimerTask in the API.
 

AgentPaper

From the depths, I come.
Reaction score
107
Well, it's the same in C++, and similar languages, isn't it?
 

Xienoph

You can change this now in User CP.
Reaction score
43
How similar is "similar"?
In C, no.
In C++, you can pass a method around (well, sort of, you pass around the pointer to the method). But the syntax is horrendous.
In C#, you can use delegates. But I think that's effectively creating a class like Java, though it is less tedious.
In JavaScript, yes you can pass functions around.
In Python, yes you can.
In JASS (obviously this has to be included) yes, but it's limited. You can't store the passed functions in an array, for example.
 

AgentPaper

From the depths, I come.
Reaction score
107
Hmm, well more to hand, how exactly do I pass a function along in java then? I make a class, put the function in it, and then pass along that class? Or pass along an instance of that class, anyways. Then, I would do something like: passedClass.function().

However, then I would have to make a new class for each function I want to pass along, wouldn't I? Or rather, a sub-class.
 

Xienoph

You can change this now in User CP.
Reaction score
43
Yes, you make a new class with the function you want to pass along in it, and pass long an instance of that class.

And yes, you'll need to make a new class for every function. But to save some time, you can make anonymous classes that implement the same interface. You can use the Runnable interface, and put the function you want to pass along as a run method.
 

AgentPaper

From the depths, I come.
Reaction score
107
So, if I made a bunch of classes with the same interface, IE runnable, then I would pass along that class like this?

waitThreadMethod(Runnable waitClass, int time)
 

AgentPaper

From the depths, I come.
Reaction score
107
Oh, I haven't tried it yet. I don't need any other waits at the moment, and I have a lot of other coding to do. Maybe I'll get to it later this week. :rolleyes:
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top