Question: I'm trying to implement web application and using a singleton object so I would be able to inject it to use it in other classes.
I want to open a new object every time the user opens a tab or similar in the browser. For example:

```
public class Singleton
{
    public string Id{get;set;}
    public Singleton(){Id=Guid.NewGuid().ToString();)
}

in startup:
services.AddSingleton<Singleton>(s=>new Singleton());

```

In some other class:

```
public class OtherClass{
    private readonly Singleton singleton;
    public OtherClass(Singleton singleton)
    {
        this.singleton = singleton;
    }
}

```

**The problem is that every tab has the same Id... I need a different id for every tab in the browser...**

Thanks!

Answer: 
I wanted to limit the number of possible threads along with not loosing any message. This requirement of mine was not fulfilled from the existing answers and I found another way to do it. Hence, posting it as an answer:

---

I made a Executor Bean as follows:

```
@Bean(name = "CustomAsyncExecutor")
public Executor customThreadPoolTaskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5);
    executor.setMaxPoolSize(5);
    executor.setQueueCapacity(0);
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.setThreadNamePrefix("Async_Thread_");
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;
}

```

And then using 

```
@Async("CustomAsyncExecutor")
public void methodName(){
....
}

```

Given that when the threads are busy & when queue is full, new tasks get rejected, 

> 
> executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy())
> 
> 
> 

helped me that when my 5 threads are busy, my invoker thread will execute the task & since my invoker thread is within the async function, it won't take any new tasks. Thus, I won't loose my tasks without increasing the queue size.
According to how ThreadPoolExecutor works, the 11th task will be rejected because when the queue is full, the executor try to increase the pool size and that if it cannot do it because the max value was reached, it rejects the task.  

You can find the information [there in the Spring documentation](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/integration.html#scheduling-task-executor) : 

> 
> The main idea is that, when a task is submitted, the executor first
>  tries to use a free thread if the number of active threads is
>  currently less than the core size. If the core size has been reached,
>  the task is added to the queue, as long as its capacity has not yet
>  been reached. Only then, if the queue’s capacity has been reached,
>  does the executor create a new thread beyond the core size. If the max
>  size has also been reached, then the executor rejects the task.
> 
> 
> 

About your requirement : 

> 
> My Requirement is to not exeed a fix number of threads spawned using
>  @Async method and to make the caller wait if max number of threads are
>  reached. Will this be achieved if I use ConcurrentTaskExecutor with a
>  fixed thread pool of a particular size?
> 
> 
> 

So increase the queue size consequently and leave the same values for core and max pool size. You could also use an unbounded queue that is the default value for the queue size parameter but beware because it could lead to `OutOfMemoryError` if too many tasks are stacked in the queue.