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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:29:58+00:00 2026-05-28T02:29:58+00:00

I got a code here, when it runs it creats and starts a new

  • 0

I got a code here, when it runs it creats and starts a new thread that prints a word per sec, after 5 sec the main method stops the thread. So the program will print 5 words and it does….but not on my homecomputer only on my laptop. On my home computer it prints 6 times, why?

public class Main {
  public static void main (String [] args){
    try {
      T1 t1 = new T1();
      System.out.println("Creating and staring thread 1\n");
      Thread.sleep(5000);
      t1.stopThread();
    } catch(InterruptedException ie) {}
  }
}

public class T1 extends Thread{

  private boolean alive = true;

  public T1(){
    start();
  }

  public void run(){
    while(alive){
      try {
        System.out.println("Tråd T1: Tråd 1");
        Thread.sleep(1000);
      } catch(InterruptedException ie) {}
    }
  }
  public void stopThread(){
    alive = false;
  }
}
  • 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-05-28T02:29:59+00:00Added an answer on May 28, 2026 at 2:29 am

    You are lucky that your program stops printing at all. You have a program that has undefined behavior and it could run forever on some machines. You must make alive volatile, otherwise there is no guarantee that your secondary thread will ever see the change made to alive in the main thread.

    Look at the end of the Java language specification chapter on memory; they basically give your example as something that must not be done.

    That being said, you might still get 6 printed lines instead of 5 from the inaccuracy of Thread.sleep.

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

Sidebar

Related Questions

I've got this code here: SqlCommand CodeStatus = new SqlCommand(SQL, DB); DB.Open(); Reader =
I've got some code here that works great on IPv4 machines, but on our
I've got a program that worked until recently. The offending code is shown here:
I've got some code that runs on every (nearly) every admin request but doesn't
Got this code from here http://www.irunmywebsite.com/raphael/drawtool2.php . Runs great in IE when I tested
I've got a thread (specifically an NSOperation) that runs to load some images for
Got this line of code here but its not working. private void Button_Click(object sender,
I have got the following code from here to read an Excel file using
Here is link where i got a code for web-page content fetching. But i
first post here, and probably an easy one. I've got the code from Processing's

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.