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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:04:26+00:00 2026-06-13T20:04:26+00:00

I am observing a strange behavior in Java threads where they are seemingly running

  • 0

I am observing a strange behavior in Java threads where they are seemingly running in a sequential fashion. Following is the code snippet

class ThreadUnsafeClass {

    private static int y;

    public static void incrementY() {
        y++;
    }

    public static int getY() {
        return y;
    }

}

public class MultiThreadedClass implements Runnable {
    int threadId;

    MultiThreadedClass (int threadId) {
        this.threadId = threadId;
    }

    @Override
    public void run() {

        System.out.println("Number:"+ threadId + ";Thread Unsafe Old Value:" + ThreadUnsafeClass.getY());

        try {
            if (threadId == 1 || threadId ==2) {
                Thread.sleep(60000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace(); 
        }
        ThreadUnsafeClass.incrementY();

        System.out.println("Number:"+ threadId + ";Thread Unsafe New Value:" + ThreadUnsafeClass.getY());
    }

    public static void main (String []args) {
        MultiThreadedClass thread1 = new MultiThreadedClass(1);
        MultiThreadedClass thread2 = new MultiThreadedClass(2);
        MultiThreadedClass thread3 = new MultiThreadedClass(3);
        MultiThreadedClass thread4 = new MultiThreadedClass(4);
        thread1.run();
        thread2.run();
        thread3.run();
        thread4.run();
   }
}

Following is the output of the same:

Number:1;Thread Unsafe Old Value:0
Number:1;Thread Unsafe New Value:1
Number:2;Thread Unsafe Old Value:1
Number:2;Thread Unsafe New Value:2
Number:3;Thread Unsafe Old Value:2
Number:3;Thread Unsafe New Value:3
Number:4;Thread Unsafe Old Value:3
Number:4;Thread Unsafe New Value:4

From the above output, it may be noticed that even when threads 1 and 2 are waiting for a reasonable amount of time i.e. 60s threads 3 and 4 still execute only when they have finished executing. Why aren’t the threads executing in parallel and when one thread sleeps, why isn’t the other thread going ahead with its execution? Am I going wrong somewhere?

  • 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-13T20:04:29+00:00Added an answer on June 13, 2026 at 8:04 pm

    You need to create a Thread referencing the Runnable and call start(), not run() on the actual class. Making something Runnable doesn’t mean it’s automatically going to run in a separate thread.

    e.g.

    (new Thread(myRunnable)).start()
    

    Why the separation between Runnable and Thread ? I can take a Runnable and drop it into a thread pool. The next available thread will run that Runnable. (e.g. see the Java Executor framework) Runnable only indicates that an object is available for threading.

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

Sidebar

Related Questions

I'm observing some strange behavior when using the F# interactive interpreter. Running the following
I'm observing some strange behavior in my code and I'm trying to track down
I have a dataset with ~20M rows and I'm observing the following behavior. The
I'm observing some strange behavior with a local variable within a nested for loop
’m observing a strange behavior from my application which I hope you can explain
I'm observing a strange behavior scheduling a job in Quartz using a CronTrigger that
I am observing a strange bug in some of my code which I suspect
I am observing some strange behavior in case of DateTime.ParseExact method with the Swedish
Using OneNote 2010 doing some C# interop stuff. Observing some very strange behavior, which
I'm observing a very strange behaviour with an entity class and loading an object

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.