• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

why this is printing value 2(two times)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class MyThread extends Thread
{

private int value = 1;


public void run()
{
System.out.print(value);
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
}
System.out.print(value);
}

public void changeValue()
{
value = 2;
}
Public static void main(String[] args)
{
MyThread ob= new MyThread();
ob.start();
on.ak();
}

}
 
Rancher
Posts: 1049
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does not, it will not even compile as you posted it. Here is a cleanup of what you posted


This does not ever reach changeValue, it is code that is never accessed. Your code prints out 11, that is "1" twice as you ask it to do so in your two print statements--once each statement. print will put the requested values on the line and keep the resulting placement for the next print, as opposed to printlin which causes a carriage return and line feed.
 
You didn't tell me he was so big. Unlike this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic