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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:51:13+00:00 2026-06-18T00:51:13+00:00

//$Id$ import java.util.logging.Level; import java.util.logging.Logger; public class ThreadSafe { public static final Logger LOGGER

  • 0
    //$Id$    
import java.util.logging.Level;
import java.util.logging.Logger;

public class ThreadSafe {

    public static final Logger LOGGER = Logger.getLogger(ThreadSafe.class.getName());

    public static int  random(int num){
        LOGGER.log(Level.INFO,"Entered Num : {0}",num);
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            LOGGER.log(Level.INFO,"Interrupted Exception");
        }
        return num + 2;
    }

    public static void main(String[] args){
        for(int threads=1;threads<100;threads++){
            final int number = threads;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    int val = ThreadSafe.random(number);
                    System.out.println("Excepted Value = " + (number+2) + " Returned Value = " + val);
                }

            },"Thread : "+threads);
            thread.start();
        }
    }


}

Output:

30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 2
30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 45
30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 44
30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 43
30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 42
30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 38
Excepted Value = 47 Returned Value = 47
30 Jan, 2013 11:41:26 PM com.zoho.learn.test.ThreadSafe random
INFO: Entered Num : 41
Excepted Value = 44 Returned Value = 44
Excepted Value = 46 Returned Value = 46

I am excepting multi threads accessing a same method at same time. What happens if we access a static methods at same time?

When race condition occurs?

What is shared-state?

Please correct me If I am wrong.

  • 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-18T00:51:14+00:00Added an answer on June 18, 2026 at 12:51 am

    The only piece of code (except run()) executed concurrently is:

    public static int  random(int num){
        LOGGER.log(Level.INFO,"Entered Num : {0}",num);
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            LOGGER.log(Level.INFO,"Interrupted Exception");
        }
        return num + 2;
    }
    

    Stripping logging (which is thread-safe in every sane implementation), sleeping (which only affects current thread) and exception handling, this is what you are left with:

    public static int  random(int num){
        return num + 2;
    }
    

    Not only num argument is private to each thread (each thread has its own stack memory), but also it’s never modified. Thus, race condition cannot occur in your program.

    There is no shared state (global variables) in your code, race condition is only possible when one thread modifies shared data while other reads it.

    You want to see a race condition? Here you go!

    private volatile int globalNum;
    
    public static int  random(int num){
        globalNum = num;
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            LOGGER.log(Level.INFO,"Interrupted Exception");
        }
        return globalNum + 2;
    }
    

    Last seconds from disaster:

    1. Thread 1 enters random(42), assigning globalNum = 42 and goes to sleep

    2. Thread 2 enters random(17), assigning globalNum = 17 and goes to sleep

    3. Thread 1 wakes up reads current value of globalNum (which is 17) and returns 19 instead of 44 as expected.

    See also

    • Are non-synchronised static methods thread safe if they don't modify static class variables?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: package test; import java.util.logging.Level; import java.util.logging.Logger; class Data{ int ar[]=new
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class InversionCounter { public static void main(String[]
package ewa; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.logging.Level; import java.util.logging.Logger; import java.math.BigInteger;
import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger;
package mp1practice; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level;
import java.util.Arrays; import java.util.ArrayList; import java.util.Scanner; import java.util.Collections; import java.util.List; public class TennisTournament {

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.