This week's book giveaway is in the Programmer Certification forum. We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
class A extends Thread { private int i; public void run() {i = 1;} public static void main(String[] args) { A a = new A(); a.start(); System.out.print(a.i); }}
//i got only 1 as answers when the possiblity to get 0.when it will happen? //where to test? [ June 30, 2004: Message edited by: giribabu venugopal ]
class Go extends Thread { public void run() { try {sleep(10000);} catch (InterruptedException ie){} } public static void main(String[] args) { Go a1 = new Go(); long startTime = System.currentTimeMillis(); a1.start(); System.out.print(System.currentTimeMillis() - startTime); }}
//3.program i didn't answer. out put is:in as jdk1.4.2 ---------- Compile ---------- javac: invalid flag: Go Usage: javac <options> <source files> where possible options include: -g Generate all debugging info -g:none Generate no debugging info -g:{lines,vars,source} Generate only some debugging info -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath <path> Specify where to find user class files -sourcepath <path> Specify where to find input source files -bootclasspath <path> Override location of bootstrap class files -extdirs <dirs> Override location of installed extensions -d <directory> Specify where to place generated class files -encoding <encoding> Specify character encoding used by source files -source <release> Provide source compatibility with specified release -target <release> Generate class files for specific VM version -help Print a synopsis of standard options
Normal Termination Output completed (0 sec consumed).
Tha call to the thread start method only puts the thread into a runnable state, it does not guarantee that it will start at that exact moment. It is possible that the next line executes before the thread starts in which case, the variable i has not been assigned the value of 1 yet. In my test, it did print a zero value.
There is no problem with the program. It should compile and run without error. Based on the error message from the compiler, it appears that you typed the following at the command line.
javac Go
If your program is contained in a file named Go.java, then you should compile it using the following command line.
javac Go.java
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
I once met a man from Nantucket. He had a tiny ad
Gift giving made easy with the permaculture playing cards