[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 The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top