I am trying to complete an experiment for a university project. I want to run it in parallel, using multiple cores, so that I can increase the sample size. To achieve this I have been making up for several (up to 7, but I have tried to use something as 2) Java thread and executing my class in those threads at once I have 8 cores in my PC.
The problem I have is that Java seems random how these threads are executed. All 7 threads start right away they run asynchronous for some time. In a typical run, maybe 3 of them will end the expected time, a fourth may end after a few minutes and the last 3 does not end at all.
The experimental class has a certain clock-length clock (not CPU-time clock). This factor is out of my control, so I need to run my threads together on different cores.
The following code snippet shows the method that I used to create and kick the thread. It is obvious that I am not calling the class using for my experiment and if you copy it and run it yourself, you will see that it works just fine, I provided it to display it simply That I am creating and using threads. I am looking for answers for the days and I do not know that I am doing anything wrong.
This is a test class that my experimental class adds some string data to ensure a long enough running process.
Public Class Thread Testclose Runnabel {@Override Public Zero Run} {Thread.currentThread (). SetPriority (Thread.MAX_PRIORITY); System.out.println ("This thread is running"); Int i = 0; String A = "A"; While (i <25) {a = a + a; I ++; } System.out.println ("This thread is running fine"); }}
How is it said:
Private static zero ThreatTestMethod () {thread [] thread = new thread [7]; Int i = 0; While (i & lt; threads.length) {thread [i] = new thread (new thread test class); Formula [i] .start (); I ++; Threads [3] .isAlive () || Thread [4] .isAlive (thread) [4] .sAlive (thread [0] .isAlive () || thread [1] .isAlive () || thread [2] .isAlive () || thread [3] ].) || Threads [5] .isAlive () || Threads [6] .isAlive ()) {try {Thread. Sleep (5000); } Grip (Interrupted ejection) {System.out.println ("Interrupted exception occurred"); }}}
I understand that Java should automatically use all the cores, and when I execute my thread, then all available cores should be used generally What happens is that when I start my experiment then it is not happening, can I do anything to move together on different cores?
Your example code is OK.
If some of your threads are not returning, then they are blocking (for example on a reading), write (), wait ()), or they are stuck in a loop; Like there is no return of any other program. Attach the debugger, or just get a stack trace dump, to know what they are doing.
The Java API does not make any sense to specify how the core is allocated. This implementation is dependent, depending on both Java implementation and operating system.
However In practice, you will find that as long as you have properly up-to-date Java, thread will spread between the core.
Comments
Post a Comment