Note that there are some explanatory texts on larger screens.

plurals
  1. POMultithreading in Java
    text
    copied!<p>Hey all<br /> I have a problem with multithreading in java. This is my scenario. I have a class,which name is LocalTime. This class should catch the current time of system, and via a method,which name is getCurrentTime, return this value to a Form. This is the code of this class <br /></p> <pre><code>public class LocalTime implements Runnable { private String currentTime=null; Thread t; private long time; private long h; private long m; private long s; public LocalTime() { t=new Thread(this,"current Time"); t.start(); } public synchronized void run() { try { for(;;){ Thread.sleep(1000); time = System.currentTimeMillis()/1000; h = (time / 3600) % 24; m = (time / 60) % 60; s = time % 60; this.currentTime=h+":"+m+":"+s; } } catch (InterruptedException ex) { Logger.getLogger(LocalTime.class.getName()).log(Level.SEVERE, null, ex); } } public String getCurrentTime(){ return this.currentTime; } } </code></pre> <p>and this is the code of main form:</p> <pre><code>public MainForm1() { initComponents(); LocalTime l=new LocalTime(); this.jLabel1.setText(l.getCurrentTime());//show the current time //The rest of code } </code></pre> <p><br /> now my major problem is the <em>jLabel1</em> text which shows current system time does not update each 1 second. If I insert <em>for(;;)</em> before the line of <em>jLabel1.setText(l.getCurrentTime())</em> the rest of code won't be reacheable. what should I to correct this error? please suggest me solution with these assumptions. I know that I can run a thread in <em>MainForm1</em>, too but I want some solution to this problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload