Producer Consumer Pattern - With Java Example

  Рет қаралды 27,968

Jakob Jenkov

Jakob Jenkov

Күн бұрын

Пікірлер: 41
@1056AD
@1056AD 3 жыл бұрын
Gets used everywhere in the industry .. such a simple concept yet an indispensable one. Thanks for picking it up
@JakobJenkov
@JakobJenkov 3 жыл бұрын
You are welcome :-) ... and I agree, it's a very useful pattern know and be able to recognize!
@singhvertika8167
@singhvertika8167 3 жыл бұрын
Thanks Jakob for this video that just made these complex concepts so much easier for us to understand
@JakobJenkov
@JakobJenkov 3 жыл бұрын
You are welcome ! ... Great to hear!
@contactdi8426
@contactdi8426 3 жыл бұрын
Thanks a ton Jakon for such a useful and awesome series! You are such a awesome teacher.
@JakobJenkov
@JakobJenkov 3 жыл бұрын
Thank you for your kind words !
@seydaozdemir
@seydaozdemir 3 жыл бұрын
Thank you Jacob. This series about Threads well explained.
@JakobJenkov
@JakobJenkov 3 жыл бұрын
You are welcome! :-) ... glad you find it useful!
@vikassharma2662
@vikassharma2662 3 жыл бұрын
Good video with detailed explanation , keep up the work jakob
@JakobJenkov
@JakobJenkov 3 жыл бұрын
Thanks :-)
@jakhongirrasulov2329
@jakhongirrasulov2329 Жыл бұрын
Thank you so much. I've been learning a lot from you.
@JakobJenkov
@JakobJenkov Жыл бұрын
That is great to hear ! Then my efforts are not completely wasted :-)
@vilastadoori
@vilastadoori 2 жыл бұрын
This is awesome Jenkov . God bless you!!
@JakobJenkov
@JakobJenkov 2 жыл бұрын
Thank you ! I am glad you found it useful! :-)
@mostinho7
@mostinho7 2 жыл бұрын
Thank you great video
@JakobJenkov
@JakobJenkov 2 жыл бұрын
You are welcome :-)
@kafychannel
@kafychannel Жыл бұрын
great tutorials thanks !
@JakobJenkov
@JakobJenkov Жыл бұрын
You are welcome :-)
@nirmalgurjar8181
@nirmalgurjar8181 3 күн бұрын
Is blockingQueue thread safe or synchronized or safe to use without volatile or locking mechanism ?
@JakobJenkov
@JakobJenkov 2 күн бұрын
BlockingQueue is ThreadSafe, yes.
@quantum598
@quantum598 3 жыл бұрын
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 :/
@JakobJenkov
@JakobJenkov 3 жыл бұрын
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.
@h.psrivastav9205
@h.psrivastav9205 Жыл бұрын
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); } } } } }
@fabricioaraujo7642
@fabricioaraujo7642 3 жыл бұрын
thanks Jakob :)
@JakobJenkov
@JakobJenkov 3 жыл бұрын
You are welcome :-)
@alexandrufilipescu1301
@alexandrufilipescu1301 2 жыл бұрын
Hello, I was wondering why the consumer threads are alternating? What makes them follow that behaviour? Thank you!
@JakobJenkov
@JakobJenkov 2 жыл бұрын
The threads run at the same time. They will most likely not be perfectly alternating - only interleaving at a semi-random order.
@alexandrufilipescu1301
@alexandrufilipescu1301 2 жыл бұрын
@@JakobJenkov Thank you for explaining the mechanism!!
@mohamedazizlakhal9368
@mohamedazizlakhal9368 Жыл бұрын
Thanks a lot!
@JakobJenkov
@JakobJenkov Жыл бұрын
Thank you! And you are welcome! :-)
@sectorseven4771
@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
@JakobJenkov Жыл бұрын
There is possible also the ForkAndJoinPool in Java. jenkov.com/tutorials/java-util-concurrent/java-fork-and-join-forkjoinpool.html
@rinkutekchandani7978
@rinkutekchandani7978 2 жыл бұрын
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
@JakobJenkov
@JakobJenkov 2 жыл бұрын
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.
@bbc168able
@bbc168able 2 жыл бұрын
Thanks, great videos. apologies for clicking on dislike accidentally instead of like, don't know how to revert.
@JakobJenkov
@JakobJenkov 2 жыл бұрын
Thanks - you just click the dislike button one more time, then you revert it.
@quangtuan202
@quangtuan202 3 жыл бұрын
Can you please add more videos from basic to advanced level?
@JakobJenkov
@JakobJenkov 3 жыл бұрын
What exactly do you mean with "from basic to advanced level" ?
@PaulO-ym5dm
@PaulO-ym5dm 2 жыл бұрын
goat
@JakobJenkov
@JakobJenkov 2 жыл бұрын
goat?
@Karupin-and-chill
@Karupin-and-chill Жыл бұрын
@@JakobJenkov it means greatest of all time
Compare and Swap in Java
24:21
Jakob Jenkov
Рет қаралды 16 М.
The Java Memory Model - The Basics
23:41
Jakob Jenkov
Рет қаралды 128 М.
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3,3 МЛН
小天使和小丑太会演了!#小丑#天使#家庭#搞笑
00:25
家庭搞笑日记
Рет қаралды 42 МЛН
SHAPALAQ 6 серия / 3 часть #aminkavitaminka #aminak #aminokka #расулшоу
00:59
Аминка Витаминка
Рет қаралды 2,4 МЛН
Life hack 😂 Watermelon magic box! #shorts by Leisi Crazy
00:17
Leisi Crazy
Рет қаралды 55 МЛН
Шаблоны Java  Producer Consumer
28:28
Java Vision
Рет қаралды 3,8 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 833 М.
Java BlockingQueue
17:48
Jakob Jenkov
Рет қаралды 40 М.
Java Lock
28:51
Jakob Jenkov
Рет қаралды 46 М.
Thread Signaling in Java
23:26
Jakob Jenkov
Рет қаралды 12 М.
Introduction to Java Semaphores - Java Programming
15:23
Will Tollefson
Рет қаралды 8 М.
Master Go Programming With These Concurrency Patterns (in 40 minutes)
46:15
34. Thread Pools in Java | ThreadPoolExecutor Framework | Multithreading Part6
1:16:55
Concept && Coding - by Shrayansh
Рет қаралды 38 М.
Java ThreadLocal
14:36
Jakob Jenkov
Рет қаралды 36 М.
Как мы играем в игры 😂
00:20
МЯТНАЯ ФАНТА
Рет қаралды 3,3 МЛН