我一直在使用线程最近,并且只是想要的东西的建议。我将把函数代码放在这里来解决任何含糊的问题。如何使用线程运行一个简单的函数
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