我一直在使用線程最近,並且只是想要的東西的建議。我將把函數代碼放在這裏來解決任何含糊的問題。如何使用線程運行一個簡單的函數
private void sort() throws FileNotFoundException, InterruptedException{
int i;
int largest = data.get(0) ;
int n = fullsize;//data.getsize
int [ ] tmp = new int [ n ] ;
for (i = 1; i < n ; i++)
if (largest < data.get(i))
largest = data .get(i) ;
int [ ] count = new int [ largest+1] ;
for (i = 0 ; i <= largest; i++)
count [ i ] = 0 ;
for (i = 0 ; i < n ; i++)
count [ data .get(i) ]++;
for (i =0+ 1 ; i <= largest; i++)
{
count [ i ] =count[i]+count[i-1];
output= output.concat(Integer.toString(count[i]));
}
System.out.print("Thread "+Thread.currentThread().getId()+":"+ output+"\n");
/* for(int b=0; i<count.length;b++)
System.out.print(count[b]);*/
for (i=n-1; i >= 0; i--)
{
tmp [count[data.get(i)] -1] = data.get(i);
count[data.get(i)]--;
}
for (i =0 ; i < n ; i++)
{
data.add(i, tmp[i]);
}
}
這個函數基本上以相當複雜的方式對鏈表進行排序,我不得不使用這個函數。 這就是我想要做的多線程功能。但現在我的問題是,你會怎麼做,每個線程的工作量差不多呢?我有點想過把數組分成幾部分,然後發送每個部分按線程排序?但我不確定這是否是這樣做的。任何正確的方向都會很棒。
如果拆分數組成零件,然後將零件進行分類,你將不得不合並結果一起得到最終結果。 –
是的,但多數民衆贊成的問題比,我有一堆或排序陣列,然後我不得不再次排序,一旦我合併他們 – jambuls