• 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

1Z0-819, question about threads.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm studying for the java 11 oracle exam using OCP Study Guides books from sybex. I have a question about threads in a loop, follow my code.


When I compile and Run this code, I have "Before thread" printed as first, which is expcted, because the main thread is independent of my new thread called "thread". In Runnable lambda, I have the Thread.sleep, which my new thread sleep. However, how my thread prints all numbers from 0 to 10? The for loop terminates first that my thread sleep and wake up. My thread retakes the for looping?
 
Marshal
Posts: 26134
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nicholas Barbosa wrote:However, how my thread prints all numbers from 0 to 10?



That's not how I read the code. The way I read it: it submits 10 Runnables to the ExecutorService. Each of the Runnables prints one number.
 
Nicholas Barbosa
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Nicholas Barbosa wrote:However, how my thread prints all numbers from 0 to 10?



That's not how I read the code. The way I read it: it submits 10 Runnables to the ExecutorService. Each of the Runnables prints one number.



Hi Paul,

Its only one thread-executor, my object have the atomic property. Thats operation is being carried out in a single unit of execution. Any thread that try to interfere, will wait the end of atomic process.

but anyway, the thread retakes the looping?
 
Paul Clapham
Marshal
Posts: 26134
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nicholas Barbosa wrote:Its only one thread-executor, my object have the atomic property. Thats operation is being carried out in a single unit of execution. Any thread that try to interfere, will wait the end of atomic process.

but anyway, the thread retakes the looping?



I'm sorry, my English isn't good enough to understand that last question.

But anyway, yes, there's only one ExecutorService. And it runs each of the 10 Runnable objects, probably one at a time sequentially. That may or may not be the "single unit of execution" which you referred to, I don't know exactly what you mean by that, but each of the 10 Runnable objects writes exactly 1 number to System.out. What you mean by "threads interfering", I also can't tell.

Perhaps you could try explaining things differently? Right now I can't tell what your question is.
 
Nicholas Barbosa
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the thread executor retakes the looping?Because I call Thread.sleep() and the loop terminates first than executor.
 
Paul Clapham
Marshal
Posts: 26134
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, I still don't understand what it means to "retake looping".

Instead let me try again with an explanation of what happens. The ExecutorService is given 10 Runnable objects to execute. This happens almost immediately as there's a loop which gives the objects to the ExecutorService. In case it isn't clear, the loop doesn't execute the Runnable objects in any way.

When the ExecutorService executes one of those objects, the object sleeps for a while and then increments/prints the value of the AtomicInteger.
 
Nicholas Barbosa
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I'm sorry, I still don't understand what it means to "retake looping".

Instead let me try again with an explanation of what happens. The ExecutorService is given 10 Runnable objects to execute. This happens almost immediately as there's a loop which gives the objects to the ExecutorService. In case it isn't clear, the loop doesn't execute the Runnable objects in any way.

When the ExecutorService executes one of those objects, the object sleeps for a while and then increments/prints the value of the AtomicInteger.



There is one exectuorService which has 10 tasks to perform, creating 10 threads for each task. How you can see, I created a pool of threads using newCachedThreadPool() from Executors factory class.I'm ordering the thread that will execute the next runnable task, slleep for a specified number of times, if I compile and run that code without atomic property, we would have many threads overwriting the result of another thread. By the way I used the AtomicInteger class to make this operation thread-safe. Any thread trying to access the count variable while an atomic operation is in process will have to wait until the atomic operation is complete.So, our operation is running in a single unit of execution.

However we have a delay(Thread.sleep) to submit another task to the single unit of execution and the for ends first than our executorService complete all tasks.
 
Saloon Keeper
Posts: 4167
160
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are (probably) 12 threads running: 11 threads created by the ExecutorService, and 1 thread that executes main. main does two rhings: it sends 11 Runnables to the Executor, printing 'before thread' for each submitted Runnable (and that is done in the blink of an eye), so main is finished while all the tasks are sleeping.

You see a funny outcome if you replace 'cachedThreadPool' by, say, FixedThreadPool(2). For instance:
 
Paul Clapham
Marshal
Posts: 26134
77
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nicholas Barbosa wrote:So, our operation is running in a single unit of execution.



I had to google "unit of execution" and the descriptions I got were extremely vague. You mean something specific by that but I don't know what. Whatever it means, there are ten threads being processed by the ExecutorService so describing that as a "unit of execution" doesn't mean much.

However we have a delay(Thread.sleep) to submit another task to the single unit of execution



No. The delay is part of the task which is submitted.

and the for ends first than our executorService complete all tasks.



Yes, that's true. There is a for-loop which submits 10 tasks to the service and terminates. And then the service will complete the tasks, which involve sleeping for a second and then incrementing an AtomicInteger.

You seem to be concerned that incrementing the AtomicInteger object will cause other threads to wait. That's true, if the other threads happen to be trying to also increment the AtomicInteger object at the exact same instant. But since it takes less than a microsecond to increment the AtomicInteger, that isn't going to delay processing in any significant way.
 
Nicholas Barbosa
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,now I understood my doubts.Briefly, Threads in AtomicClasses like in monitor/lock(synchronized), of course with some differences ,waits the previous thread to execute your own task(increment and report).

Thank you all!

Paul,single unit of execution is an analogy of next thread wait the previous thread execute the task without any interference by another thread.
 
Paul Clapham
Marshal
Posts: 26134
77
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like we understand each other better now.

Yes, it's only the incrementing of AtomicInteger which can block another thread momentarily. Sleeping (of course) doesn't block any other threads, and as for System.out.println: it might block other threads but I don't know, I don't think that is documented anywhere.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic