Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Runnable and Thread  (Read 2922 times)
0 Members and 1 Guest are viewing this topic.
Offline VIrtueeL
« Posted 2015-05-28 19:40:02 »


My Runnable!
anyhelp it only prints out the seconds not what i print out here
1  
2  
3  
4  
5  
6  
7  
8  
9  
         LooterTimer timeTast1 = new LooterTimer(10, new Runnable(){

            @Override
            public void run() {
               System.out.println("200"); <-- I WANT THIS TO PRINT OUT!
            }

           
         });



LooterTimer

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
package my.tdl.managers;

public class LooterTimer implements Runnable{

   private int defaultSeconds;
   private int seconds;
   private boolean hasCompleted;
   private boolean running;
   
   private Thread thread;
   
   public LooterTimer(int seconds, Runnable runnable) {
      this.seconds = seconds;
      this.defaultSeconds = seconds;
      start();
   }

   @Override
   public void run() {
      if(!running){
         running = true;
      }
     
      while(running){
         if(seconds != 0){
            System.out.println(seconds+" seconds left!"); <-- already printing!
            seconds -= 1;
            try {
               Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
         }
         
         if(seconds == 0){
            stop();
         }
      }
     
     
   }


   public void stop() {
      running = false;
      hasCompleted = true;
      seconds = defaultSeconds;
      thread.stop();
   }

   public void start() {
      running = false;
      hasCompleted = false;
      seconds = defaultSeconds;
      if(thread == null){
         thread = new Thread(this);
         thread.start();
      }
   }
   
   public boolean isRunning() {
      return running;
   }


   public boolean hasCompleted() {
      return hasCompleted;
   }

}


i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Opiop
« Reply #1 - Posted 2015-05-28 19:50:59 »

Wait, whoa. Why do you have a thread inside a Runnable?

You're calling start upon the class's runnable implementation, the object "thread" is never called because "start" is a function within the runnable interface.
Offline VIrtueeL
« Reply #2 - Posted 2015-05-28 19:52:38 »

Wait, whoa. Why do you have a thread inside a Runnable?

You're calling start upon the class's runnable implementation, the object "thread" is never called because "start" is a function within the runnable interface.

could you tell me how to do this stuff xD never used threading that mutch xD give example on how to achieve this

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2015-05-28 19:53:23 »

You pass a Runnable instance to a constructor of a class implementing Runnable, and never store/use the argument. How's the Thread going to find out about the instance, and execute it? That, and a dozen race conditions, which can be ignored for now, given the nature of the previous issue. You might take a step back and consider mastering the essentials of the java language instead of plowing through like this.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline VIrtueeL
« Reply #4 - Posted 2015-05-28 19:59:17 »

You pass a Runnable instance to a constructor of a class implementing Runnable, and never using said instance. How's the Thread going to find out about the instance, and execute it? That, and a dozen race conditions, which can be ignored for now, given the nature of the previous issue. You might take a step back and consider mastering the essentials of the java language instead of plowing through like this.

this is why jgo sucks balls everybody can just say "learn essentials" jesus....
instead of helping...

i do devlogs and i do tutorials check em out
https://www.youtube.com/user/InidDevo
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2015-05-28 20:02:44 »

Sometimes it's better to be direct. From the code you provided it's pretty clear you're in way over your head. It's too early to mess with concurrency when you struggle with reference passing and concepts like implementing interfaces vs. using them. Most importantly, what you are trying to do (performing an action after a certain delay) should not be done with threads at all. All you need is to save a timestamp, compare it to the current time, and execute a task when a certain amount of time has elapsed - a few frames later.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 486
Exp: 7 years



« Reply #6 - Posted 2015-05-28 20:14:14 »

this is why jgo sucks balls everybody can just say "learn essentials" jesus....
instead of helping...

This attitude won't help you as a programmer, or as anything in life really.
I would say we are helping.

Riven is correct, timestamps are the way to go here, I even wrote a simple system for doing this here:
http://www.java-gaming.org/topics/how-to-delay-somthing-without-stopping-the-gameloop/33752/msg/317647/view.html#msg317647
Offline Sickan
« Reply #7 - Posted 2015-05-29 10:38:11 »

They say learn the essentials because you need to know the essentials before you can apply them. If you don't know how to apply the basic concepts of the language, you're going to have a hard time using it. Yes, people here help but they're not going to hold your hand while you learn the Java language - if that's what you want then you're probably not going to find it here. On the other hand, once you start needing help with more advanced things (like the optimal game loop) it is a great help because of the combined experience and knowledge.
Offline KevinWorkman

« JGO Plugged Duke »


Medals: 288
Projects: 12
Exp: 12 years


HappyCoding.io - Coding Tutorials!


« Reply #8 - Posted 2015-05-29 16:21:31 »

this is why jgo sucks balls everybody can just say "learn essentials" jesus....
instead of helping...

Then log out.

Riven gave you the answer, and if you didn't understand it, you could have come back with a technical question asking for clarification. Instead, you're throwing an internet tantrum about being told that you should take a step back and start with something simpler, which is true.

I was going to expand on what Riven said and break it down into more easily digestible pieces, but after seeing this, I think I'll spend my time on more productive and receptive posters instead.

Good luck.

HappyCoding.io - Coding Tutorials!
Happy Coding forum - Come say hello!
Offline Soulfoam
« Reply #9 - Posted 2015-05-29 17:10:41 »

You pass a Runnable instance to a constructor of a class implementing Runnable, and never using said instance. How's the Thread going to find out about the instance, and execute it? That, and a dozen race conditions, which can be ignored for now, given the nature of the previous issue. You might take a step back and consider mastering the essentials of the java language instead of plowing through like this.

this is why jgo sucks balls everybody can just say "learn essentials" jesus....
instead of helping...

The fact your signature says you do "tutorials" scares me massively.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DarkCart

JGO Kernel


Medals: 124
Projects: 9
Exp: 50 years


It's all in the mind, y'know.


« Reply #10 - Posted 2015-05-29 17:23:54 »

The fact your signature says you do "tutorials" scares me massively.

^ This.
To kind of put this into perspective, think of me (DarkCart) doing a series of tutorials on something like LWJGL. I don't know the first thing about doing something like this (as of right now I can barely get a quad and triangle to render at the same time), so teaching people to try to do something I don't know how to do myself is kind of, frankly, useless.

The darkest of carts.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (581 views)
2019-09-04 15:33:17

hadezbladez (5510 views)
2018-11-16 13:46:03

hadezbladez (2402 views)
2018-11-16 13:41:33

hadezbladez (5772 views)
2018-11-16 13:35:35

hadezbladez (1223 views)
2018-11-16 13:32:03

EgonOlsen (4661 views)
2018-06-10 19:43:48

EgonOlsen (5682 views)
2018-06-10 19:43:44

EgonOlsen (3198 views)
2018-06-10 19:43:20

DesertCoockie (4095 views)
2018-05-13 18:23:11

nelsongames (5115 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!