Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Challenge Semaphore | High-level Synchronization Mechanisms
Multithreading in Java
course content

Contenuti del Corso

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

book
Challenge Semaphore

Task

Imagine you have a resource that can be accessed by only two threads at a time. Your task is to create a program that uses a semaphore to control access to this resource.

Conditions:

  1. You have a Resourceclass with a use()method that simulates the use of a resource, such as printing a message and introducing a delay;

  2. You have a Workerclass that implementsRunnable; in its run()method, it will attempt to access the resource using a semaphore;

  3. In the SemaphoreExampleclass, you have a main()method that initializes a semaphore with permits for two threads, along with multiple worker threads that will attempt to access the resource;

  4. Ensure that no more than two threads can use the resource at any given time.

How should the program work?

You have 5 threads that want to execute the use() method simultaneously, but with a Semaphore, you need to limit the simultaneous execution to 2 threads. As soon as a thread releases the Semaphore, another thread can begin executing this method. For better clarity in this scenario, it’s recommended to introduce a delay within the use() method.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

book
Challenge Semaphore

Task

Imagine you have a resource that can be accessed by only two threads at a time. Your task is to create a program that uses a semaphore to control access to this resource.

Conditions:

  1. You have a Resourceclass with a use()method that simulates the use of a resource, such as printing a message and introducing a delay;

  2. You have a Workerclass that implementsRunnable; in its run()method, it will attempt to access the resource using a semaphore;

  3. In the SemaphoreExampleclass, you have a main()method that initializes a semaphore with permits for two threads, along with multiple worker threads that will attempt to access the resource;

  4. Ensure that no more than two threads can use the resource at any given time.

How should the program work?

You have 5 threads that want to execute the use() method simultaneously, but with a Semaphore, you need to limit the simultaneous execution to 2 threads. As soon as a thread releases the Semaphore, another thread can begin executing this method. For better clarity in this scenario, it’s recommended to introduce a delay within the use() method.

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 4
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt