Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-10-2007, 04:43 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Explanation bout threading and concurrency?
Can someone explain bout the matter please?
I have been reading some books bout concurrency topics, but haven't quite understand the basic
Why must we invoke the start() method to run the thread if the method thats gonna run the thread is instead the run() method?Why don't we just use the run() method?
A further explanation bout priorities, usage of yield and sleep, synchronized, volatile or any other keywords that may come in place, followed by some conceptual examples or codes bout it would be really helpful

Sorry if i asked too much

Thanks
CruxBlack
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-10-2007, 12:33 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
The start and interrupt methods are in the Thread class api. (One of) The Thread constructor(s) takes a Runnable as an argument. The Runnable interface has a single method: run. We don't call run directly but start up a Thread which does the work of calling the run method which runs until completion/the method returns or ends.
Here is an example of how you can put these together to make a timer.
Code:
import java.awt.event.*; import javax.swing.*; public class ConcurrencyExample implements Runnable, ActionListener { JLabel label; Thread thread; boolean running; long delay = 1000; int count = 0; public void run() { while(running) { try { Thread.sleep(delay); } catch(InterruptedException e) { System.out.println("interrupted"); stop(); } label.setText("count = " + count++); } } private void start() { if(!running) { running = true; thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } } public void stop() { running = false; if(thread != null) thread.interrupt(); // wake up thread = null; } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("START")) start(); if(ac.equals("STOP")) stop(); } private JLabel getCenter() { label = new JLabel("", JLabel.CENTER); return label; } private JPanel getControls() { String[] ids = { "start", "stop" }; JPanel panel = new JPanel(); for(int j = 0; j < ids.length; j++) { JButton button = new JButton(ids[j]); button.setActionCommand(ids[j].toUpperCase()); button.addActionListener(this); panel.add(button); } return panel; } public static void main(String[] args) { ConcurrencyExample test = new ConcurrencyExample(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getCenter()); f.getContentPane().add(test.getControls(), "Last"); f.setSize(300,160); f.setLocation(200,200); f.setVisible(true); } }
For background and discussion see the basic lesson on threads in java Lesson: Concurrency and the more specialized Lesson: Concurrency in Swing which deals with the event dispatch thread/single thread issues in swing.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java threading Eranga Advanced Java 2 03-13-2008 07:30 AM
Class explanation mcal New To Java 1 02-05-2008 08:50 PM
Threading prob.. banie Java Applets 0 02-05-2008 08:30 AM
need a little explanation cew27 New To Java 7 12-14-2007 01:39 AM
I need didactic explanation Eric New To Java 2 07-02-2007 07:37 AM


All times are GMT +3. The time now is 11:30 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org