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 5 of 5

Thread: Suspend - Resume: What is the correct way to do this without deprecated methods?

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Suspend - Resume: What is the correct way to do this without deprecated methods?

    Hello there.

    I had a simple program which used the "suspend" and "resume" methods from Java's Thread class.
    Those two methods are deprecated now because they are deadlock prone.
    I would like to re-write my code to not use those deprecated methods anymore but I am not 100% sure how the ideal implementation would look like.

    I have a very simple test program which shows the behaviour I want to achieve.
    public class ThreadTest {
     
    	private static Thread t1;
     
    	public static void main(String[] args) {
    		t1 = new Thread() {
    			public void run() {
    				threadLoop();
    			}
    		};
    		System.out.println("Start");
    		t1.start();
    	}
     
    	private static void threadLoop() {
    		int i = 0;
    		while (true) {
    			i++;
    			if (i == Integer.MAX_VALUE) {
    				openPopup();
    				i = 0;
    			}
    		}
    	}
     
    	private static void openPopup() {
    		Popup popup = new Popup("ALARM!", 
    				"This is an important error message!", 
    				256, 
    				256);
    		popup.addWindowListener(new WindowListener() {
    			public void windowOpened(WindowEvent arg0) {
    			}
    			public void windowIconified(WindowEvent arg0) {
    			}
    			public void windowDeiconified(WindowEvent arg0) {
    			}
    			public void windowDeactivated(WindowEvent arg0) {
    			}
    			public void windowClosing(WindowEvent arg0) {
    			}
    			public void windowClosed(WindowEvent arg0) {
    				t1.resume();
    			}
    			public void windowActivated(WindowEvent arg0) {
    			}
    		});
    		t1.suspend();
    	}
     
    	private static class Popup extends JFrame {
    		... // this is not important
    	}
    }

    Thank you very much in advance.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Suspend - Resume: What is the correct way to do this without deprecated methods?

    Your answers are here, near the middle of the page, but the whole page is good reading.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Suspend - Resume: What is the correct way to do this without deprecated methods?

    Thank you.
    That looks a little bit complicated for this simple functionality. Especially since it is absolutely sure that no deadlock can occur in this particular situation.
    I think I will think about this some more.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Suspend - Resume: What is the correct way to do this without deprecated methods?

    it's not really that complicated. The thread runs when a sentinel value says "Go" and pauses when it says "Stop." Pretty simple, really.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Suspend - Resume: What is the correct way to do this without deprecated methods?

    Well, its an additional boolean variable, a loop nested with an if and a synchronized block.
    If you compare this to suspend / resume, its quite a bit more complicated. Bloats the code up and its not too obvious what is happening there if you just glance over it.

Similar Threads

  1. resume
    By game06 in forum The Cafe
    Replies: 5
    Last Post: July 12th, 2013, 02:01 AM
  2. java method deprecation <deprecated>
    By jaffars.sk in forum Java Theory & Questions
    Replies: 1
    Last Post: December 16th, 2011, 05:46 PM
  3. Resume Application in Original State
    By WhiteSoup12 in forum Android Development
    Replies: 0
    Last Post: November 22nd, 2011, 08:11 PM
  4. StopWatch resume problem??
    By Tobbe129 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 19th, 2011, 02:21 PM