multithreading - Java Thread Yielding/ Starvation Problem -


i'm writing code run multithreaded bank. first create array of threads 1 program, pass them thread runs loop start them. part of application, have cpu intensive method runs series of loops within 1 another. problem is, reason not yielding way think should. here code running threads:

public void run(){     this.setpriority(max_priority);     int count = 0;      while(count<transactions.length){         int copy = count;             if(transactions[copy] instanceof jumbler){                 system.out.println(copy + " jumbler.");             }             else{                 system.out.println(copy  + " not jumbler");             }         transactions[copy].run();         count++;     }      } 

then here jumbler run method:

    public void run(){     system.out.println("running jumbler");     thread.yield();     thread.currentthread().yield();     try{         thread.currentthread().sleep(5000);     }catch(interruptedexception e){}     //this.setpriority(min_priority);     system.out.println("still running.");     thread.yield();     nums = new int[1000];     int = 0;      do{         thread.yield();          for(int x=0;x<1000;x++){             thread.yield();             //system.out.println("in loop");             nums[x]=(int)(math.random()*10000)+1;             for(int y = 0;y<1000;y++){                 thread.yield();                 //system.out.println("in the loop");                 for(int z = 0;z<100;z++){                     thread.yield();                 }             }         }         thread.yield();         i++;         system.out.println(whichjumble + ": " + i);     }while(i<1000); } 

so, problem want yield, allowing main method continue running more threads, blocks , waits jumbler complete (which takes long time). idea why happen or how fix it?

i suppose issue comes transactions[copy].run(); in main loop. 1 calls run method directly not in system thread. instead start thread transactions[copy].start();.


Comments