• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Threads

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so how can i set time to j label?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();

 
look! it's a bird! it's a plane! It's .... a teeny tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic