Results 1 to 5 of 5

Thread: Threads!

  1. #1
    robc3129 is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Lightbulb Threads!

    Hi.

    I'm currently writing an application which contains a couple of classes which i want to run in separate threads so that they can both process a request at the same time and update their relevant GUI windows. I need both classes (threads) to available to my whole application (so i'm currently using static vars and methods) as they are used to process data which could be called in a few places in my application. So, just to clarify, i need class1 and class2 to run in two threads but to also be accessible to multiple classes in my application.

    Can anyone tell me if this is good practice and also how i could go about doing this?

    Cheers!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Threads!

    Typically, threads are most useful for background processing of events and such without affecting responsiveness of a primary application (like a GUI). If you want to do it with threads I would check out some of the concurrency classes such as ExecutorService. You may also want to consider writing an event handler and registering event listeners.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,853
    Rep Power
    5

    Default Re: Threads!

    Like everything else, there is of course a trail for it: Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Threads!

    The direction others have pointed you is good - to your one question though:
    Can anyone tell me if this is good practice...?
    I would say no. Unless you are handling all the synchronization, concurrent access to static resources will either throw ConcurrentModificationExceptions all over the place, or leave your data in inconsistent states. Multi-threaded programming is difficult, and can be simplified by limiting and modularizing the data the threads work with. Locks and synchronization can also lead to slowdown or deadlock in certain situations which is obviously counterproductive.

    If you understand the basic multi-threading concepts/problems (like concurrent modification errors, synchronization, semaphores, deadlock, starvation, etc...) then java has nice tools built in to assist. If not - you should go back and do some more research before you go down this path.

  5. #5
    robc3129 is offline Member
    Join Date
    Sep 2013
    Posts
    6
    Rep Power
    0

    Default Re: Threads!

    Thanks for the advice everyone! Some good points for me to think about there!

Similar Threads

  1. Threads
    By ChaoticXknight in forum New To Java
    Replies: 2
    Last Post: 10-26-2012, 03:57 PM
  2. Threads per Connection or Threads per Request
    By Felic in forum New To Java
    Replies: 4
    Last Post: 11-22-2011, 09:15 PM
  3. How to use two threads
    By rajan701v in forum Threads and Synchronization
    Replies: 2
    Last Post: 07-22-2011, 05:47 PM
  4. The right way of using threads
    By N00Bie in forum New To Java
    Replies: 9
    Last Post: 02-20-2011, 04:39 PM
  5. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •