Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calender.getInstance() issue with ScheduledThreadPoolExecutor

KonradZuseMar 22 2013 — edited Apr 4 2013
cross posted here http://www.coderanch.com/t/607695/java/java/Calender-getInstance-system-time-autoupdate#2775075

I have been trying to get the time, and update it(like making my own digital clock).


Okay, so I tried using the ScheduledThreadPoolExecutor, and it worked great! The only issue is that I'm having trouble updating the calender.

NOTE: Lambda expressions are used as "->" so runnable and run have been removed.
public class Timer extends Application
{
  //  Timer t = new Timer();
    Calendar rightNow =  Calendar.getInstance();
    @Override
    public void start(Stage primaryStage)
    {
        Button btn = new Button();
        
         ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1);
		System.out.println(stpe.getCorePoolSize());
		
                stpe.scheduleAtFixedRate(() -> {
                               btn.setText("" + rightNow.getTime());
                               System.out.println(rightNow.getTime());
   //rightNow = Calendar.getInstance();
                },0, 1,TimeUnit.SECONDS);
                
        btn.setOnAction((ActionEvent event) -> {
          //  rightNow = Calendar.getInstance();
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        
        Scene scene = new Scene(root, 300, 250);
        
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
Basically if I run it normally it will just return the instance at that one second, and wont update with rightNow.getTime() and will just pop out the same time constantly.

If I try to set
 rightNow = Calendar.getInstance();
inside the setFixedRate it wont run the scheduler, and if I click the button after I've seen it update the instance a few times, it will then stop right away.

Why exactly is my ScheduledThreadPoolExecutor breaking when I'm getting my instance? Is there another way I should go about this maybe?
This post has been answered by James_D on Mar 22 2013
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2013
Added on Mar 22 2013
23 comments
976 views