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

  • Home
  • SEARCH
  • 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 7847503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:55:10+00:00 2026-06-02T17:55:10+00:00

Please see the program below public class TestVolatile implements Runnable { public static volatile

  • 0

Please see the program below

public class TestVolatile implements Runnable {

    public static volatile int counter;
    public static String lock = "lock";

    public static void main(String[] args) {
        Thread t1 = new Thread(new TestVolatile(),"Thread-1");
        Thread t2 = new Thread(new TestVolatile(),"Thread-2");
        t1.start();
        t2.start();
    }

    public void run() {
        synchronized(this) {
            System.out.println(Thread.currentThread()+"-"+counter);
            counter++;
        }
    }
}

If I run this program multiple times, I get 3 different results.

first is

Thread[Thread-1,5,main]-0
Thread[Thread-2,5,main]-0

second is

Thread[Thread-1,5,main]-0
Thread[Thread-2,5,main]-1

third is

Thread[Thread-1,5,main]-1
Thread[Thread-2,5,main]-0

But if change the lock object from “this” to “lock”, I get 2 different results

first is

Thread[Thread-1,5,main]-0
Thread[Thread-2,5,main]-1

second is

Thread[Thread-1,5,main]-1
Thread[Thread-2,5,main]-0

My assumption when writing the program was that in either case the “counter” should never come 0 in both statements.
Can somebody explain?

  • 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-02T17:55:12+00:00Added an answer on June 2, 2026 at 5:55 pm

    You create two TestVolatile objects. The “this” keyword refers to the TestVolatile object being run in the thread. Thus you do not synchronize on the same object in the first example.

    If you change the code like this, then the first example starts working:

    public static void main(String[] args) {
        TestVolatile testVolatile = new TestVolatile();
        Thread t1 = new Thread(testVolatile,"Thread-1");
        Thread t2 = new Thread(testVolatile,"Thread-2");
        t1.start();
        t2.start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see following methods. public static ProductsCollection GetDummyData(int? customerId, int? supplierId) { try {
See below code first please. using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I am using Axis2 for developing webservices . Please see my program below :
I'm writing a simple program - please see below for my code with comments.
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
Please see the sample xml given below. As per a requirement, I need to
PLEASE SEE THE EDIT BELOW, THE REPORT SEEMS TO USE CACHED DATA? I cant
I am using c# for programming, please see below code: <p><b> <%=ResourceFile.GetResourceString(c_FreeLiveOnlineProgramInterest)%> </b>&nbsp;<font color=red>*</font>&nbsp;
Please see below pseudocode method. If the call to readUrls() throws an exception, doesn't

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.