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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:13:41+00:00 2026-06-13T15:13:41+00:00

I want to simulate TCP in Java. For that I have multiple threads, like

  • 0

I want to simulate TCP in Java.

For that I have multiple threads, like sender and receiver threads for every TCP connection.

My problem is, I want to pause (like Thread.sleep()) threads for microseconds time interval. So that I can simulate flow control, where sender thread will block for microseconds before sending next packet, and at same time CPU can be used by receiving and data processing threads. But I couldn’t find any methods which perform sleep() or wait() for microsecond or nanosecond resolution. How can I block (pause) threads in Java with micro or nanosecond resolution?

I found System.nanoTime() method, but there no methods for blocking threads for specified micro or nano seconds. If there any such methods, then please let me know. System.nanoTime() just gives relative time interval in nanoseconds.

I can use System.nanoTime() to perform nanosecond delay in threads using busy-loop, but that will waste CPU, which could have been used for receiving data thread or processing thread.

Another confusing question:

By surfing internet, I found Thread.sleep() or wait() methods blocks for at least specified milliseconds or in multiple of 10ms, whichever is less, without thread interrupt, in Windows System. But when I run sample examples, I found very different results. Like threads are sleeping less than specified milliseconds. Some threads are giving me 100 microsecond sleep. Is it error in time measured? Is not System.nanoTime() that much accurate? See below example where I am getting very different results. Thread priorities are also not giving predicted results.

public class MyClass {
    public static void main(String args []){
        Runnable r = new Runnable() {
            public void run() {
                long s = System.nanoTime();
                //Thread.yield();  // Try for yield, need to check - in how much time high priority thread will be scheduled back after yielding
                try{
                    Thread.sleep(1);
                }catch(Exception e){

                }
                long e = System.nanoTime() - s;
                System.out.println(Thread.currentThread().getName()+e);
            }
        };

        Thread t1 = new Thread(r, "Thread T1: ");
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();

        Thread t2 = new Thread(r, "Thread T2: ");
        t2.setPriority(Thread.MIN_PRIORITY);
        t2.start();

        Thread t3 = new Thread(r, "Thread T3: ");
        t3.setPriority(Thread.NORM_PRIORITY);
        t3.start();

        Thread t4 = new Thread(r, "Thread T4: ");
        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();

        long s = System.nanoTime();
        try{
            Thread.sleep(1);
        }catch(Exception e){

        }
        long e = System.nanoTime() - s;
        System.out.println("Main: "+e);
    }
}

In my System with Intel Core i5, 2.53 GHz, I am getting outputs similar like below in multiple runs. *New-line in below output is not printed in this post, I have copied output from console, but its giving me new-line in my my console, that’s not problem.

Thread T1: 736682
Thread T3: 700212
Thread T2: 478559
Main: 548257
Thread T4: 458299

Second run –

Thread T3: 27555
Thread T2: 139799
Thread T1: 152361
Main: 93199
Thread T4: 29986

Third run –

Thread T4: 724525
Thread T2: 1050319
Main: 752486
Thread T1: 941316
Thread T3: 1000883

Fourth run –

Thread T1: 174243
Thread T2: 166543
Thread T3: 1005745
Main: 1023170
Thread T4: 915787

In above runs, I am getting microseconds blocking. :O

Please guide me on this.

Apologize for long post. Thank you for reading whole post.

  • 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-06-13T15:13:43+00:00Added an answer on June 13, 2026 at 3:13 pm

    You’re at the mercy of the OS thread scheduler, and you can’t do much about it. Just because you sleep for a very short time doesn’t mean the scheduler will awake your thread as soon as it has finished sleeping. And thread priorities are only a hint, without any guarantee.

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

Sidebar

Related Questions

I have an application where I want to simulate the connection between a device
I have multiple buttons on a page and I want to simulate a click
I want to simulate mouse movement every x seconds. For that, I'll use a
I have a JFrame on which I want to simulate a countdown (like a
I want to simulate values that represent a geometric distribution. The plot I have
I have the following problem: I want to simulate some control engineering system. As
I have an emulator that I want to simulate the loss of a data
I want to simulate stroking a carpet, so you would have a graphic of
I would want to simulate the behavior of a table with div. I have
I have a 32GB memory machine and I want to simulate trying out one

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.