'parallel and Threading in TestNG,Selenium

i have a project which runs in parallel mode (parallel=classes). in @beforeClass method i have written to open a web-driver. Now lets take an example: lets say, i have 9 classes in my test which i'll run in parallel mode with thread count =3.

when execution is started selenium first runs @beforeclass for all 9 classes(which in-turn opens all 9 web-drivers and then executes 3 classes at a time( based on 3 threads). why it does like that. how do i make sure to start only 3 classes finish it then start next three.

since selenium opens 9 drivers at first, it consumes system RAM and CPU whenever i have lot of classes in a single test

Any idea how to configure testNG/selenium to open/run only 3(thread count) classes , finish it then open next 3 ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source