Gets used everywhere in the industry .. such a simple concept yet an indispensable one. Thanks for picking it up
@JakobJenkov3 жыл бұрын
You are welcome :-) ... and I agree, it's a very useful pattern know and be able to recognize!
@mohamedazizlakhal93682 жыл бұрын
Thanks a lot!
@JakobJenkov2 жыл бұрын
Thank you! And you are welcome! :-)
@contactdi84263 жыл бұрын
Thanks a ton Jakon for such a useful and awesome series! You are such a awesome teacher.
@JakobJenkov3 жыл бұрын
Thank you for your kind words !
@seydaozdemir3 жыл бұрын
Thank you Jacob. This series about Threads well explained.
@JakobJenkov3 жыл бұрын
You are welcome! :-) ... glad you find it useful!
@singhvertika81673 жыл бұрын
Thanks Jakob for this video that just made these complex concepts so much easier for us to understand
@JakobJenkov3 жыл бұрын
You are welcome ! ... Great to hear!
@jakhongirrasulov2329 Жыл бұрын
Thank you so much. I've been learning a lot from you.
@JakobJenkov Жыл бұрын
That is great to hear ! Then my efforts are not completely wasted :-)
@vilastadoori2 жыл бұрын
This is awesome Jenkov . God bless you!!
@JakobJenkov2 жыл бұрын
Thank you ! I am glad you found it useful! :-)
@vikassharma26623 жыл бұрын
Good video with detailed explanation , keep up the work jakob
@JakobJenkov3 жыл бұрын
Thanks :-)
@mostinho73 жыл бұрын
Thank you great video
@JakobJenkov3 жыл бұрын
You are welcome :-)
@kafychannel Жыл бұрын
great tutorials thanks !
@JakobJenkov Жыл бұрын
You are welcome :-)
@PaulO-ym5dm2 жыл бұрын
goat
@JakobJenkov2 жыл бұрын
goat?
@Karupin-and-chill2 жыл бұрын
@@JakobJenkov it means greatest of all time
@alexandrufilipescu13012 жыл бұрын
Hello, I was wondering why the consumer threads are alternating? What makes them follow that behaviour? Thank you!
@JakobJenkov2 жыл бұрын
The threads run at the same time. They will most likely not be perfectly alternating - only interleaving at a semi-random order.
@alexandrufilipescu13012 жыл бұрын
@@JakobJenkov Thank you for explaining the mechanism!!
@nirmalgurjar81813 ай бұрын
Is blockingQueue thread safe or synchronized or safe to use without volatile or locking mechanism ?
@JakobJenkov3 ай бұрын
BlockingQueue is ThreadSafe, yes.
@quantum5983 жыл бұрын
Jakob the videos are brilliant, I have a question as I was trying to tackle this on my own...say we have 3 threads (0,1,2) and we want thread 0 to msg all threads (1 and 2) and then we have thread 1 doing the same with threads 0 and 2...and so forth, how do we go about that? I know its kind of similar to the producer/consumer example but in this case each thread can produce/consume messages as they arrive? curious to know as I have found few resources on Stack as well as here :/
@JakobJenkov3 жыл бұрын
One way to solve this problem would be to give each thread an inbound queue (BlockingQueue) via which they can be messaged. Thread 1 can then send messages (objects) to the queues of thread 2 and thread 3, and thread 2 can write to the queues of thread 1 and thread 3 etc.
@Love-way_HH Жыл бұрын
import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class Main { private static final int NUM_THREADS = 3; private static final BlockingQueue[] queues = new BlockingQueue[NUM_THREADS]; public static void main(String[] args) { for (int i = 0; i < NUM_THREADS; i++) { queues[i] = new LinkedBlockingQueue(); } for (int i = 0; i < NUM_THREADS; i++) { final int threadId = i; Thread thread = new Thread(() -> threadFunc(threadId)); thread.start(); } } private static void threadFunc(int threadId) { while (true) { // Check for messages while (!queues[threadId].isEmpty()) { String msg = queues[threadId].poll(); System.out.println("Thread " + threadId + " received: " + msg); } // Send messages to other threads for (int i = 0; i < NUM_THREADS; i++) { if (i != threadId) { queues[i].offer("Hello from thread " + threadId); } } } } }
@sectorseven4771 Жыл бұрын
How would one retrieve and merge the results of the consumers once finished? Let's say a user (could be many users) clicks a button and produces many tasks to do like query a website and retrieve information that needs to be merged/summed up when all tasks for that user have been consumed. I am currently testing a multi user & threading environment with a custom thread pool, but this thread pool is working in a 'first come first serve' manner that is not very performant to say the least and I thought to give the consumer producer pattern a try, but I do not know if this is the correct approach. Would highly appreciate some advice from your side.
@JakobJenkov Жыл бұрын
There is possible also the ForkAndJoinPool in Java. jenkov.com/tutorials/java-util-concurrent/java-fork-and-join-forkjoinpool.html
@rinkutekchandani79783 жыл бұрын
Thanks Jacob for the video. I have a question,if we want to consume messages in the order they were produced using two consumers,how could we do that
@JakobJenkov2 жыл бұрын
Well, the two consumers would have to take messages from the same queue - so the queue keeps the sequence of the messages. However, there is no way to guarantee which of the threads finishes their tasks first. If Thread A and Thread B both take a task right after each other from the queue, even if Thread B took its task after Thread A, it might still finish it before Thread A finishes its task. If you want to guarantee that something post-processing happens in a certain order, you will need to implement that yourself.
@fabricioaraujo76423 жыл бұрын
thanks Jakob :)
@JakobJenkov3 жыл бұрын
You are welcome :-)
@quangtuan2023 жыл бұрын
Can you please add more videos from basic to advanced level?
@JakobJenkov3 жыл бұрын
What exactly do you mean with "from basic to advanced level" ?
@bbc168able3 жыл бұрын
Thanks, great videos. apologies for clicking on dislike accidentally instead of like, don't know how to revert.
@JakobJenkov3 жыл бұрын
Thanks - you just click the dislike button one more time, then you revert it.