Results 1 to 3 of 3

Thread: Java threading

  1. #1
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts

    Java threading

    I'm working on some coursework and this is a small part of it which I thought would be a good idea to implement.

    I have two classes, Simulator and SimulatorView, the latter displays the GUI and the backend is run from the former. I'm passing the current instance of Simulator to SimulatorView. The reason for this is because I have a JMenu with JMenuItem's in it which will invoke methods on Simulator. Obviously the events are passed to actionPerformed() which is carried out in the dispatch thread. So if I run the application and then click on File > Run it will then halt the program until Simulator has finished and then display the final output. As it's a simulation of a real life scenario the simulation goes through a set number of states and usually you can watch it carry out through each state but as the action is thrown into the dispatch thread it'll halt.
    Does this mean I have to use Thread and would it be easy to implement? My main problem is implementing everything because I have many options in the GUI and I need the simulation to run no matter what else I do. In the actionPerformed() method I have if else statements just checking what JMenuItem was pressed and then carrying out the necessary action.

    Should I use Thread? Or Timer? Or (1.6 will implement it) SwingWorker? I've never implemented threads so I'm a bit stuck at the moment on how to implement it correctly.

    Any help would be appreciated but don't give me big hints as it is for coursework

  2. #2
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Just read up on interface Runnable.
    To err is human. To really foul things up ... you need a computer.

  3. #3
    Senior Member
    Join Date
    Aug 2005
    Posts
    213
    Thanks
    0
    Thanked
    0 times in 0 posts
    As you are using a GUI I assume you are using the SWING API. If this is the case, whenever you spawn a thread in a Swing app, you should do it using the swing dispatch thread and not spawn your own thread. This will ensure that the swing app will correctly handle any screen updates and interactions.

    So if you want a task to run in another thread you should look up the runnable interface and use SwingUtilities.invokeLater. This will place the task in the swing dispatch thread and execute it there.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I.M.P.L (Java / DirectShow question)
    By Purple in forum Software
    Replies: 2
    Last Post: 15-08-2005, 05:09 PM
  2. Key Java source code to be opened
    By Steve in forum HEXUS News
    Replies: 0
    Last Post: 27-06-2005, 10:54 AM
  3. Replies: 0
    Last Post: 14-06-2005, 12:02 PM
  4. ATi + Java + BSOD?
    By Kezzer in forum PC Hardware and Components
    Replies: 1
    Last Post: 11-06-2005, 01:26 PM
  5. Java Linux
    By Raz316 in forum Software
    Replies: 8
    Last Post: 06-04-2005, 09:35 AM

Posting Permissions

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