Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Whizlab question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From Q55 of the diagnostic exam:


class TestThread extends Thread
{

private int i;
public void run()
{
i++;
}

public static void main(String[] args)
{
TestThread a = new TestThread();
a.run();
System.out.print(a.i);
a.start();
System.out.print(a.i);
}
}

Should the answer be "prints 11" or "prints 11 or 12"?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"prints 11 or 12" as you can't be 100% sure whether the second print will be executed before or after i is incremented in the separate thread

Sok
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first a.run() will call the run() method and print 1.this will not start a new thread of execution.'
next if a.start() is called then it will call the run() method thus making the value of i=2 with o/p 12,but if the thread scheduler does not start this
new thread then run() will not be invoked thus the value of i will
remain 1,thus printing 11
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!