Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4342256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:35:38+00:00 2026-05-21T11:35:38+00:00

I have the following sample piece of code, public class test implements Runnable{ public

  • 0

I have the following sample piece of code,

public class test implements Runnable{

  public void run()
  {
    synchronized(this)
    {
        for(int i=0;i<10000;i++)
            System.out.println(i);
    }
  }
  public static void main(String args[]) throws Exception
  {
    Thread t=new Thread(new test());
    t.start();

    //Thread.sleep(100);
    t.suspend();
    System.out.println("Hi");
  }
}

I have the following requirement,When the thread is suspended it must execute all the iterations of the loop without getting suspended. I felt that synchronized is for this purpose but it didnot solve the problem. When i suspend a thread if it is in a synchronized block then it must get suspended only after the block is executed. What java construct can be used for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:35:39+00:00Added an answer on May 21, 2026 at 11:35 am

    In Java, when a piece of code is in synchronized block, only one thread can execute this block at once. In order to suspend a thread only after it has finished its synchronized block, just synchronized over the same variable:

    public class Test implements Runnable {
        public void run() {
            synchronized(this) { 
                // some work
            }
        }
    
        public static void main(String args[]) throws Exception {
            Test target = new Test();
            Thread t = new Thread(target);
            t.start();
    
            synchronized(target) {
                t.suspend();
            }
            System.out.println("Hi");
        }
    }
    

    Note that both blocks in run() and main() synchronize over the same variable (instance of Test). Now the code in synchronized block in main() will be executed only after the work in run() is done.

    (Please note however, that there is some race condition here: you cannot guarantee that after t.start() the code in run() will actually execute before the next line of code in main())

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of snippet Class Sample { Obj_Class1 o1; Obj_Class2 o2;};
I have the following sample code which doesn't seem to want to run. import
I have the following test sample: <Window x:Class=WpfScrollTest.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=Window1 Height=200 Width=200> <Border>
I have the following code sample that im trying to wrap my head around
I have the following code sample: float val = 16777216.0F; var badResult = Convert.ToDecimal(val);
I have the following piece of code which works fine and does what it
I have the following piece of code, executing a pretty simple MySQL query: $netnestquery
I have following piece of code in my jsp file which getSession information: <jsp:useBean
I have written the following piece of code that reads through a given xml-file
I have the following piece of code in my Spring MVC application: @RequestMapping(value =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.