3

Can anyone guide me in this Example Thread and ThreadPool What is the difference between them? Which is better to use ...? What is the drawback on ?

I used a threadpool and why use it in this case true or false ?

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

class ThreeThread implements Runnable {

    String c;
    Semaphore s1;
    Semaphore s2;

    public ThreeThread(String s, Semaphore s1, Semaphore s2) {
        this.c = s;
        this.s1 = s1;
        this.s2 = s2;
    }

    @Override
    public void run() {
        while (true) {
            try {
                s1.acquire();
                Thread.sleep(400);
                System.out.println(c);
                s2.release();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                System.out.println(e);
            }
        }
    }

    public static void main(String[] args) {
        Semaphore sm1 = new Semaphore(1);
        Semaphore sm2 = new Semaphore(0);
        Semaphore sm3 = new Semaphore(0);

        ExecutorService es = Executors.newFixedThreadPool(3);

        es.execute(new ThreeThread("1", sm1, sm2));
        es.execute(new ThreeThread("2", sm2, sm3));
        es.execute(new ThreeThread("3", sm3, sm1));
    }
}
xingbin
  • 27,410
  • 9
  • 53
  • 103
Abdo Bmz
  • 632
  • 1
  • 11
  • 24
  • 2
    Threads are heavy, i.e. expensive to create. You would use threads for long-lasting processes. If you have a lot of small tasks, you can use a thread pool. The pool allocates threads only once and re-uses them to avoid unnecessary thread creation. In regards to your example: please fix your indendation. Some explanation of what the program is supposed to to would be helpful. I see some synchronization between threads. You should avoid this in a ThreadPool since you may run into a thread starvation problem. – Turing85 Apr 22 '18 at 15:29
  • 2
    thanks for explanation but i can't understand you about fix indentation ..... ? – Abdo Bmz Apr 22 '18 at 15:33
  • 1
    Take a look at your `try`-block. – Turing85 Apr 22 '18 at 15:33
  • The best practice is to use some framework that takes care of synchronization if it's possible. – Bogdan Lukiyanchuk Apr 22 '18 at 15:34
  • @Turing85 *Why* is it expensive to create a new thread? – Shane Sepac Apr 22 '18 at 15:46
  • @Shn_Android_Dev [Why is creating a Thread said to be expensive?](https://stackoverflow.com/questions/5483047/why-is-creating-a-thread-said-to-be-expensive) – Turing85 Apr 22 '18 at 15:49
  • 1
    For the interested reader: The JDK team is working on an even more lightweight parallel model, called [Project Loom](http://cr.openjdk.java.net/~rpressler/loom/Loom-Proposal.html) – Turing85 Apr 22 '18 at 15:55

3 Answers3

3

See the doc of ThreadPoolExecutor:

Thread pools address two different problems:

they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead,

and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks.

Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks.

Community
  • 1
  • 1
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • 1
    @Shn_Android_Dev I believe this question can help you. https://stackoverflow.com/questions/5483047/why-is-creating-a-thread-said-to-be-expensive – xingbin Apr 22 '18 at 15:47
-1

Well, I thought that a ThreadPoolExecutor provided better performance for it manages a pool of threads, minimizing the overhead of instantiating a new thread, allocating memory...

And if you are going to launch thousands of threads, it gives you some queuing functionality you would have to program by yourself...

Threads & Executors are different tools, used on different scenarios... As I see it, is like asking why should I use ArrayList when I can use HashMap? They are different...

Kaka
  • 1
  • 5
-1

when to use: Threads: I want to manage thread creation, decide time of creation, and monitor, synchronization, thread cleanup, myself.

Executor: I want specialist/manager to perform these activities.

Umy Angel
  • 61
  • 1
  • 3