Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default MultiThreading Problem part 2

    I have made a code in which

    GUI class

    Data Class

    are present

    GUi calls Data class and in Data class i have started a new thread and at the same time gui interface thread is also running

    Now the problem is this i want to change the thread in Data class that is currently being executed

    So how should i close the current thread and begin a new thread.


    Java Code:
    class Data
    {
       Data()
       {
             ....
             Thread thread=new Thread(this);
              thread.start();
       }
    
        public void run()
        {
           ...........
         }
    
        public void next()
        {
             //here i have to move the next data and run the thread with the new data  by closing the current thread
        }
    }
    thanx in advance :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If the Thread is run in a SwingWorker object, then it can often be canceled by calling cancel(...) on the SwingWorker object. If it is a non-SwingWorker background thread, then you may wish to use a boolean in one of the thread's loops that uses a class field to determine if the thread should continue looping, and then set this boolean from outside the background task using a setter method.

    If these suggestions don't help, then you may want to create and post the smallest program possible that is compilable, runnable, demonstrates your problem, and has no extraneous code not related to the problem, an SSCCE Please see the link for details on how to create one of these because trust me, if your SSCCE is compliant with the specs, you'll likely get a very helpful answer from someone here quickly.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ravjot28 View Post
    I have made a code in which

    GUI class

    Data Class

    are present

    GUi calls Data class and in Data class i have started a new thread and at the same time gui interface thread is also running

    Now the problem is this i want to change the thread in Data class that is currently being executed

    So how should i close the current thread and begin a new thread.


    Java Code:
    class Data
    {
       Data()
       {
             ....
             Thread thread=new Thread(this);
              thread.start();
       }
    
        public void run()
        {
           ...........
         }
    
        public void next()
        {
             //here i have to move the next data and run the thread with the new data  by closing the current thread
        }
    }
    thanx in advance :)
    Why don't you simply feed your new data to the already running thread? Starting a new thread eats some resources too; it can be much cheaper to re-supply the already running thread.

    kind regards,

    Jos

  4. #4
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    well i am playing a mp3 and that is played using JPlayer

    so it runs its own thread too

    now the prob is that

    void run() // 1st thread
    {
    p.play(); //2nd thread
    }

    now if want to change the song i have to kill the current thread coz there is now option to kill p thread

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ravjot28 View Post
    well i am playing a mp3 and that is played using JPlayer

    so it runs its own thread too

    now the prob is that

    void run() // 1st thread
    {
    p.play(); //2nd thread
    }

    now if want to change the song i have to kill the current thread coz there is now option to kill p thread
    Doesn't a p.stop() method exists? (like for a Clip it does).

    kind regards,

    Jos

  6. #6
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    no there is no such method that is why i am thinking of closing the current thread in which p.play() is running

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What (class) type is your p? You would think that a class that has a play method should also have a stop method.

  8. #8
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    see i am using JLayer1.0 api and i am making an object of this api's class Player now there are functions like play() , play(int frames) , wait() and etc. there is no func called stop or destroy so i am thinking of killing the parent thread this can clear my obstacle easily so can you give me a way to kill the parent thread

    i am posting my code you can have look at it

    Java Code:
    import javazoom.jl.player.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    
    
    // This class playes the mp3 files.
    class MP3 extends Observable implements Runnable
    {
         .. variables
    
        void download()
        {
            ....
                thread.setName("rav");
                thread.start();            // Starting parent thread
        }
    
        public void next() throws InterruptedException
        {
          //to change the song
        }
    
    //Play file.
        public void run()   // Parent Thread
        { 
            do
            {
                try
                {
                     System.out.println(thread.getName());
                     
                        FileInputStream s=new FileInputStream(son);
                        Player p=new Player(s);
                        p.play();                                    // Thread that plays the song
                        if((p.isComplete())||(!rav))
                        {
                            son=songs[pp[++i1]];
                        }
                    
                }catch(Exception song){}
               
            }while(true);
        }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  10. #10
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    see i am playing xyz.mp3 song now i want to close xyz.mp3 how can i close it if i am not able to move that parent thread

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ravjot28 View Post
    see i am playing xyz.mp3 song now i want to close xyz.mp3 how can i close it if i am not able to move that parent thread
    What do you mean by "not able to move that parent thread"? You call close on a Player object, not a thread. The Player object should take care of its own thread.

  12. #12
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    see my frnd i have to make a local Player object in the thread so i can not close it out side the thread

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ravjot28 View Post
    see my frnd i have to make a local Player object in the thread so i can not close it out side the thread
    Why do you say this? The API mentions nothing of this.

  14. #14
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    see i have gui which controls the events now when i click on play button and if start tha Player.play() method without the thread then my gui gets inactive due to swing thread now for that i have made a thread which runs Player.play() and this solved my prob .

    Now for next song to play i want to kill the player object first and then restartt the thread this is my prob

    Thanx for replying

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I wonder if you are getting the two threads confused, Swing's EDT thread and the background thread that the player is being run on. If you call Player#play(...) on the EDT, on Swing's main thread, then yes, it stands to reason that risk freezing Swing since the play method initiates a process that takes a long time to complete. Calling close shouldn't have this problem since it should take less than a second for the player to respond to this method. If you're still worried about it, then sure, go ahead and call close on a background thread, but this still begs the question (again), have you tried calling close on the player? Have you seen what happens, whether it works or not? I don't think it's important that close be called on the same thread that play is called on, and what's more, I don't see how it is possible (unless you use an event queue on a background thread like Swing uses -- but that seems like overkill). As the Nike commercials state: "just do it". And then come back and let us know what happens.

  16. #16
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    here is my code i tried p.close() there was no response

    Java Code:
    class MP3 extends Observable implements Runnable
    {
         ...variables
    
        void download() throws FileNotFoundException, JavaLayerException
        {
            ......
                thread.setName("rav");
                thread.start();
        }
    
        public void next() throws InterruptedException
        {
           p.close();
        }
    
    
    
    //Play file.
        public void run()
        {
            do
            {
                try
                {   
                        p.play();
                        if((p.isComplete())||(!rav))
                        {
                            son=songs[pp[++i1]];
                        }
                    
                }catch(Exception song){}
                if(i1==pp.length)
                {
                    i1=0;
                }
            }while(true);
        }
    
    
    }

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ravjot28 View Post
    here is my code i tried p.close() there was no response
    These code snippets tell us little and at least for me tell me nothing of why your code isn't working. I can say this: it is likely not a thread issue. More likely you're calling p.close() on a Play object that is not the one that is currently playing music. Some investigation by you can prove or disprove this. Please let us know how you resolve this.

  18. #18
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    well thanx for you reply

    i am not able to stop the current song and p.close() is not working i have tried it but one thing i found out that when i placed a command thread.wait() in the run() method at that time the song was stopped that is why i keen interested in closing the thread rather trying to close the player object.

    So lets short it up the new question is that
    i am having a thread i know its name now i want to close that thread how can i do it?

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You have yet to let me know if you've checked that your Player reference that you're calling close refers to the same Player object that is running. Til you do that, I'm not wasting my time on theoretic thread non-issues.

  20. #20
    ravjot28 is offline Member
    Join Date
    Jun 2008
    Posts
    87
    Rep Power
    0

    Default

    Java Code:
    public class NewClass
    {
        public static void main(String args[]) throws FileNotFoundException, JavaLayerException
        {
            Player p=new Player(new FileInputStream("D:\\Multimedia\\Music\\Songs\\xyz.mp3"));
            p.play();
            p.close();
        }
    }
    now this p.close never runs until p.play() finishes his job i.e to play the song

    See basically my prob is that whenever i give a command p.play at that time songs starts playing but how to interrupt it is my prob because p.close will work fine but i need to interrupt the p.play();
    Last edited by ravjot28; 03-02-2010 at 01:54 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. MultiThreading Problem
    By ravjot28 in forum New To Java
    Replies: 8
    Last Post: 03-02-2010, 01:23 PM
  2. Log 4j Multithreading
    By joe2010 in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-31-2010, 04:48 AM
  3. problem using ObjectOutputStream in multithreading
    By sanjeevbindroo in forum Networking
    Replies: 3
    Last Post: 10-15-2009, 09:00 AM
  4. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 05:18 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
  •