• Post Reply Bookmark Topic Watch Topic
  • New Topic

why this is printing value 2(two times)  RSS feed

 
Akshith Reddy
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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();
}

}
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!