• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple Threads

 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've created two threads and first one for clock(it shows current time),second thread continuously access the database(it continuously shows data which updated by users).if we do not insert thread.sleep() one thread run longer time therefor other thread can't run. so how can i manage this two threads.




 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, I added code tags for you ... notice that it looks much nicer now...


Anyway, Swing isn't thread safe. You aren't allowed to change values of Swing component outside of the event dispatching thread. This means that line 12 of your code, the clockLabel instance, assuming that it is an instance of a JLabel, may cause issues. This may also be the reason why you think that the threads are not running -- you may have frozen your GUI.

Henry
 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so how can i set time to j label?
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lahiru nanayakkara wrote:so how can i set time to j label?


The Oracle tutorial on Swing has a section that discusses this...

http://docs.oracle.com/javase/tutorial/uiswing/

Basically, it is done via the use of the SwingUtilities and SwingWorker classes.

Henry
 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


this time i've created 3 threads and remove connection with jlabel instead for jlabel it shows through Sys.out.println(); ,All threads are running but time is skipping.




 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lahiru nanayakkara wrote: All threads are running but time is skipping.



Perhaps you can clarify what you mean by "time is skipping".

Henry
 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think now time is 12:23:01 then it shows 12:23:04 but you know it must be 12::23:02.
i know that cause ,when other threads are at running state but is their any solution ?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the cause is that you're updating the GUI several times on a thread which is not the Swing thread and expecting to see the result of each update separately. The earlier post which recommended you read the Swing threading tutorial and use a SwingWorker or something similar still applies; you didn't do that.
 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created swingworker instance and scheduled it in worker thread(tAccess = new TimeAccess()).execute()) this instance to access the time. but i need another swing worker instance to continuously access database.how to do this?

// when execute both instances ,sometimes GUI become unresponsive
tAccess = new TimeAccess()).execute()
dAccess = new DataAccess()).execute();

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!