• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on ExamLab mock

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this is my first post and I hope I am following all the rules correctly.

This is the code of question 44 on the final exam of ExamLab free mock:
The question is, what would be the result when compiling and running it.



According to the mock, the correct answers are:
- Prints "Builder Passed, "
- Will not terminate

Now, I agree that this program will not terminate, but I don't really get why "Builder Passed" is a correct answer.
Actually, I have two question here:
- Can we rely on the sleep method to really switch threads?
- And how can we be sure that the JVM won't start thread mt2 right away before executing "sbl.append("AAA");"?

I know that on a regular situation this would be the correct answer, but I believe that the correct answer should be the one for all situations, and for me it should be that the result is unpredictable. Does someone see any other reason behind the answer? What would be the correct answer if this was a question on the exam?

Thank you all for the help!
 
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I'm not sure, but...

From javadoc:
Thread.sleep(long millis):
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.

So I assume that that the processor time moves to other availables threads.

program execution:
- x = 1
- first Thread.sleep(1000) moves to the mt1 thread.
- enter case 1. No other thread can change sbf until the 'main' thread dies.
- back to 'main' thread
- x = 2
- sbl = SBLAAA
- second Thread.sleep(1000) moves to the mt2 thread.
- enter case 2. No other thread can change sbl until the 'main' thread dies.
- back to 'main' thread
- print Builder Passed,
- try to access sbf, but mt1 has the lock, so infinite execution

again ... I'm not sure
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with both Leandro Coutinho and Felipe HSilva.

output is Builder Passed and program will not terminate
but Felipe HSilva has following questions

- Can we rely on the sleep method to really switch threads?
- And how can we be sure that the JVM won't start thread mt2 right away before executing "sbl.append("AAA");"?


I have also same doubt and I think thread mt2 may start before or after executing sbl.append("AAA"); so output is unpredictable.

Let see what other people will say about this issue.
Can anybody focus on this issue please?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think the key thing here is that StringBuilder doesn't have synchronized methods, whereas StringBuffer does. That's why the main thread can operate on the StringBuilder object even if the mt2 thread has already acquired the lock on the StringBuilder object.

Refering to your questions: There is no guarantees of any kind. When a thread sleeps it simply stops running (and is put in the Sleeping state) for a period of time. This doesn't release the locks, and doesn't guarantee that any other thread will actually run. Technically the JVM is sharing your computer's CPU with the OS and any other number of system and application processes, so there is no guarantees that any other of the threads in your Java program will run while any given thread is sleeping (although in practical terms that almost always will be the case.)

And we can be sure that the JVM will start thread mt2 before the main thread modifies the sbl object (because the code is calling mt2.start() before the append operation.) But make sure you understand what start means. It doesn't mean that the run() method will start executing. It simply means that the thread mt2 will be put in the Runnable state. As for whether mt2 will start running before the append operation, it is possible but it is not guaranteed. That doesn't matter though, since StringBuilder doesn't have synchronized methods.
 
Felipe HSilva
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Thanks for all the replies. I believe we have a consensus that if we need to find an answer for all cases, the "unpredictable result" would be more adequate. So the question that remains is what should be the correct answer if I see this on the real exam? Well, if "unpredictable result" was one of the answers I would go for it. I just hope this is the way the exam's creators think.
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both Ruben Soto and Felipe HSilva for reply with nice explaination.

Hello Ruben Soto I already understood the program execution. The main thing is we can't predict when mt2 thread will run.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Ninad, but it doesn't make any difference in this case. What does make a difference in the output is whether mt1 gets to run.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Felipe HSilva wrote:Hi all,

Thanks for all the replies. I believe we have a consensus that if we need to find an answer for all cases, the "unpredictable result" would be more adequate. So the question that remains is what should be the correct answer if I see this on the real exam? Well, if "unpredictable result" was one of the answers I would go for it. I just hope this is the way the exam's creators think.


Felipe,

Technically there are two possibilities:
- If the evaluation of the x in mt1's switch gets to run before the assignment x = 2 in the main thread, and mt1 acquires the lock on sbf before the sbf.append("AAA") call in the main thread, then the output will be "Builder passed" and the program will never finish normally.
- In all other cases, the output will be "Builder passed" "Buffer passed" and the program will finish normally.

The second scenario is not very likely, due to the Thread.sleep(1000) statement before the assignment x = 2. But it is theoretically possible. If you see this kind of question in the exam you should probably take a guess using your common sense. Hopefully you won't see this kind of question in the exam though.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:
Technically there are two possibilities:
- If the evaluation of the x in mt1's switch gets to run before the assignment x = 2 in the main thread, and mt1 acquires the lock on sbf before the sbf.append("AAA") call in the main thread, then the output will be "Builder passed" and the program will never finish normally.
- In all other cases, the output will be "Builder passed" "Buffer passed" and the program will finish normally.

The second scenario is not very likely, due to the Thread.sleep(1000) statement before the assignment x = 2. But it is theoretically possible. If you see this kind of question in the exam you should probably take a guess using your common sense. Hopefully you won't see this kind of question in the exam though.



Yes I agree. Since that is theoretically possible we cannot assume (though 99.99% of time it will produce the given answer) what the answer says as correct in exam or anywhere-else.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Felipe HSilva wrote:Well, if "unpredictable result" was one of the answers I would go for it. I just hope this is the way the exam's creators think.



That's not, because there will be an 'assumption statement' given on the real exam. Unfortunately, I forgot to insert it for this question. I'll correct this with the next update.

Devaka.
reply
    Bookmark Topic Watch Topic
  • New Topic