Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Code is running in multi-threading but it is neither pausing nor resuming..........

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Smile Code is running in multi-threading but it is neither pausing nor resuming..........

    I have created a JFrame here and you will also see JPanel and JLabel. I have also added two JButton "Pause" and "Resume" over there.

    What I am trying to do is to pause and resume a child thread using JButtons, the program is running fine without any warning or errors, however, child thread is not pausing. The functions executes but it is not pausing the thread.

    I have attached two text files. All the functions are executing in "Main.java" file and "child thread" code is written child_1.java.

    Looking for quick help !!
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    Please post any code you are having problems with on the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    I have already attached the files in 1st post, the name of the files are child_1.txt and Main.txt ........

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    Post the code in the thread, not as links.
    Be sure to wrap the code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    <code>
    public class child_1 implements Runnable {
    Thread t;
    Main main;
    boolean Flag;

    child_1() {
    System.out.println("Child Thread 1: No Parameter");
    }

    child_1(Main main) {
    this.main = main;
    t = new Thread(this, "child_1");
    System.out.println("Child 1 Started");
    t.start();
    Flag = false;
    }

    synchronized public void run() {
    int k = 0;
    try {
    for (int i = 0; i < 30; i++) {
    main.label_1.setText(k + "");
    k = k + 1;
    Thread.sleep(1000);

    synchronized (this) {
    while (Flag) {
    wait();
    }
    }// /synchronized
    }// for
    }// try
    catch (InterruptedException e) {
    System.out.println("Child interrupted." + e);
    }// catch
    System.out.println("Exiting child thread 1 =>" + k);
    }// run

    synchronized void mysuspend() {
    Flag = true;
    System.out.println("Thread 1 Suspend !!");
    }

    synchronized void myresume() {
    Flag = false;
    notify();
    System.out.println("Thread 1 resumed !!");
    }
    }
    </code>
    ################################################## #######
    <code>
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Color;

    public class Main implements ActionListener {

    public JFrame frame;
    public JLabel label_1;
    public JPanel titlePanel_1;
    public JButton Pause_1;
    public JButton Resume_1;

    child_1 c1;

    Main() {
    frame = new JFrame("Frame Counter");
    frame.setVisible(1 == 1);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setSize(800, 500);
    frame.setLocation(520, 120);
    frame.setLayout(null);

    label_1 = new JLabel("My 1st Label");
    // label_1.setBounds(20, 20, 190, 30);
    // frame.add(label_1);

    titlePanel_1 = new JPanel();
    titlePanel_1.setBounds(20, 20, 200, 50);
    titlePanel_1.add(label_1);
    frame.add(titlePanel_1);
    titlePanel_1.setBackground(new Color(176, 200, 150));

    Pause_1 = new JButton("Pause");
    Pause_1.setBounds(325, 20, 100, 50);
    frame.add(Pause_1);
    Pause_1.addActionListener(this);

    Resume_1 = new JButton("Resume");
    Resume_1.setBounds(500, 20, 100, 50);
    frame.add(Resume_1);
    Resume_1.addActionListener(this);

    c1 = new child_1();

    }

    public void actionPerformed(ActionEvent ae) {
    String button = ae.getActionCommand();

    if (button.equalsIgnoreCase("Pause")) {
    try{c1.mysuspend();System.out.println("Button you Pressed==>" + button);}
    catch(Exception e){ System.out.println("Child interrupted." + e);}
    }
    if (button.equalsIgnoreCase("Resume")) {
    try{c1.myresume();System.out.println("Button you Pressed==>" + button);}
    catch(Exception e){ System.out.println("Child interrupted." + e);}
    }

    }

    public static void main(String args[]) {

    Thread t1;
    Runnable run1;
    Main main = new Main();

    run1 = new child_1(main);
    t1 = new Thread(run1);

    }
    }

    </code>

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    I have updated my post..........

  8. #8
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to pause thread using JButton

    I have updated my post again, I dont know how to go further..............

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    Did you look at it to see that it was correctly formatted?
    It should look like this
         public Scanner(Object o) {
            if (myScnr == null) {           //   Put responses here
               myScnr = new java.util.Scanner("1375 1.175 7 1 No\n");
            }
         }
         public Scanner(File f) throws FileNotFoundException {
           if(!f.exists())
              throw new FileNotFoundException(f.getName()); 
           //?????
           System.out.println(">>> No Scanner code for file: " + f);
         }


    --- Update ---

    Quote Originally Posted by pawan4angel View Post
    I am trying to pause and resume child thread using two JButtons on JFrame, looking for some help ...........

    There will be a main thread and one child thread........
    Please only start One thread per topic. Threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    I did not get it

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    I did not get it
    Please explain what "did not get it" means.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    leave it, I solved that problem at my own...............thanks for trying here..............

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Code is running in multi-threading but it is neither pausing nor resuming..........

    Please mark this thread as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. InterruptedException in multi threading
    By me_shankara in forum Threads
    Replies: 1
    Last Post: April 18th, 2013, 12:54 PM
  2. Replies: 2
    Last Post: January 6th, 2012, 10:50 PM
  3. Having Difficulties With a Multi Thread Code... Help?
    By Allicat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 12:35 AM
  4. Converting Code to Multi Thread
    By cmill_xc in forum Threads
    Replies: 1
    Last Post: December 6th, 2010, 12:39 PM
  5. Replies: 1
    Last Post: March 23rd, 2010, 02:29 AM

Tags for this Thread