I want to create dynamic number of thread which is depends on the database Number of database rows..
List list = session.createQuery("From Devices").list();
Number of thread is depends on the list.size().
I am Creating dynamic number of thread using For loop
new Thread(){public void run(){/* Task of each thread */}.start();
Is it right way to create dynamic number of thread ?? If i use shared variable do I need to define synchronized.. Any another idea how to manage thread where thread count become dynamic and depends on User.
Another question how can i define some private variable which is separated to each thread and not sheared to each other ..????
thanks
If you just need a team of threads to execute something on every row I would instead use a thread pool:
It’s much easier to submit work and manage the pool than using an array of threads.
After this, synchronization depends on the actual computation. If you are concurrently modifying some shared state (list, counter) you will need to synchronize access across threads.
Finally, to define a thread task with private state: