Results 1 to 2 of 2
- 08-10-2007, 02:43 AM #1
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
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
- 08-10-2007, 10:33 AM #2
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.
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.Java 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); } }
Similar Threads
-
Java threading
By Eranga in forum Advanced JavaReplies: 2Last Post: 03-13-2008, 05:30 AM -
Class explanation
By mcal in forum New To JavaReplies: 1Last Post: 02-05-2008, 06:50 PM -
Threading prob..
By banie in forum Java AppletsReplies: 0Last Post: 02-05-2008, 06:30 AM -
need a little explanation
By cew27 in forum New To JavaReplies: 7Last Post: 12-13-2007, 11:39 PM -
I need didactic explanation
By Eric in forum New To JavaReplies: 2Last Post: 07-02-2007, 05:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks