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 97853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:05:03+00:00 2026-05-11T00:05:03+00:00

Certain methods in Java will block until they can do something, like ServerSocket.accept() and

  • 0

Certain methods in Java will block until they can do something, like ServerSocket.accept() and InputStream.read(), but how it does this is not easy for me to find. The closest thing I can think of is a while() loop with a Thread.sleep() each time through, but the longer the sleep period, the less responsive the blocking, and the shorter the sleep, the more spinning that occurs.

I have two questions:

  1. How do various standard functions, like the ones above, block? Native code? while() loops? Something else?

  2. How should I implement methods that block?

  • 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. 2026-05-11T00:05:04+00:00Added an answer on May 11, 2026 at 12:05 am

    The essential answer to (1) is ‘don’t worry about it — the OS handles it’. Calls to things like reading from input streams are essentially wrappers around operating system calls. Under the hood inside the OS, what I think is usually happening when a call ‘blocks’ in these cases is that the OS ‘knows’ that it is waiting for a hardware interrupt from, say, the disk controller to say that such-and-such requested data is now available, and it knows that Thread X was the one that requested that data. So it doesn’t schedule in Thread X again until it receives that interrupt (or an interrupt saying ‘there was an error’ etc). (And part of the thread scheduling algorithm is then doing things like giving the waiting thread a temporary ‘boost’ of some kind when that waited-for data becomes available. Again, usually you don’t need to worry too much about this.) Or put another way: whatever the exact details of this mechanism, it’s not available to the general Java programmer.

    In (2), I would suggest thinking more about ‘how do I do Thing X, which might happen to block’. I think the answer is hardly ever that Thing You Want To Do is deliberately just ‘block’, and whatever Thing X is, there’s probably a library method/class that will do it for you. For example (links include some material I’ve written on these subjects):

    • if you want to take the next message/job when it becomes available from some queue/provider, look at blocking queues
    • if you need to control access to a shared resource with a ‘lock’ on an object, waiting for the lock to become available if necessary, consider plain old synchronized, or an explicit lock;
    • if you want to wait for one of many pooled resources to become available, look at semaphores

    I’d say that the raw wait/notify mechanism is largely deprecated with the Java 5 concurrency API. And whatever you’re doing, spinlocking is usually the very very last resort.

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

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer int printf ( const char * format, ... ); They… May 11, 2026 at 8:22 am
  • added an answer You should generate a formated list with the data to… May 11, 2026 at 8:22 am
  • added an answer How can I use Python to make sure that all… May 11, 2026 at 8:22 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.