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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:07:03+00:00 2026-06-01T03:07:03+00:00

I am looking at an example which code is: class SimpleThread extends Thread {

  • 0

I am looking at an example which code is:

class SimpleThread extends Thread {
    public SimpleThread(String str) {
        super(str);
}

public void run() {
    for (int i = 0; i < 10; i++) {
        System.out.println(i + " " + getName());
            try {
        sleep((int)(Math.random() * 1000));
        } catch (InterruptedException e) {}
    }
    System.out.println("DONE! " + getName());
    }
}

and

class TwoThreadsTest {
    public static void main (String args[]) {
        new SimpleThread("Jamaica").start();
        new SimpleThread("Fiji").start();
    }
}

My question is: is there a way each thread does its own code? For example, one thread increments a variable, while the other thread increments other variable.
Thanks.

P.S. Example’s link is: http://www.cs.nccu.edu.tw/~linw/javadoc/tutorial/java/threads/simple.html

  • 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-01T03:07:04+00:00Added an answer on June 1, 2026 at 3:07 am

    Each instance of SimpleThread has it’s own local class storage. As long as you aren’t using fields marked as static, then each thread will “do its own code”. It is much harder to synchronize values between threads.

    For example:

    class SimpleThread extends Thread {
    
        // this is local to an _instance_ of SimpleThread
        private long sleepTotal;
    
        public SimpleThread(String str) {
            super(str);
        }
    
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(i + " " + getName());
                try {
                    long toSleep = Math.random() * 1000;
                    // add it to our per-thread local total
                    sleepTotal += toSleep;
                    sleep(toSleep);
                } catch (InterruptedException e) {}
            }
            System.out.println("DONE!  " + getName());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Vehicle { public int wheels; public int lights; } class Car extends Vehicle
I'm looking for a way to overwrite for example the dialog which pops up
I'm looking for example code for setting up a connection to a bluetooth device
I have a data structure that represents C# code like this: class Namespace: string
following piece of code: class a{ public function __get($key) { if($key == 'obj') {
I am using c#. Now I am looking to create a JSONHelper class, which
I'm looking into speeding up my python code, which is all matrix math, using
Here is only an example from my code. I'm looking for a good way
BACKGROUND: I have a GridView based on a fairly complex Adapter class which extends
I'm using Ubuntu 10.04 and I'm trying to run a WxRuby example which I

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.