Results 1 to 2 of 2
  1. #1
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Question 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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.
    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);
        }
    }
    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.

Similar Threads

  1. Java threading
    By Eranga in forum Advanced Java
    Replies: 2
    Last Post: 03-13-2008, 05:30 AM
  2. Class explanation
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-05-2008, 06:50 PM
  3. Threading prob..
    By banie in forum Java Applets
    Replies: 0
    Last Post: 02-05-2008, 06:30 AM
  4. need a little explanation
    By cew27 in forum New To Java
    Replies: 7
    Last Post: 12-13-2007, 11:39 PM
  5. I need didactic explanation
    By Eric in forum New To Java
    Replies: 2
    Last Post: 07-02-2007, 05:37 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
  •