• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 23928
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: 23928
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: 23928
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 ?
 
Sheriff
Posts: 27465
88
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();

 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic