File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updated MulitThreads Problem

 
james walder
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MulitThreads Problem
i am trying to write the code for a multithread where it can sort both numeric and string data then sort the data in an array were pairs of arrays are compared and swapped if out of order any help would be great. the code so far is as follows

public class MultiThread implements Runnable{
public int countDown=4;
public int threadNumber;
private static int threadCount=0;
public int[] myarray = {3,1,4,2};
public MultiThread()
{
threadNumber =++ threadCount;
System.out.println("Making" + threadNumber);
}
public void swap(int[] myarray,int a,int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
System.out.println(a +" "+ b);
return;
}
public void run()
{
while(true)
{
System.out.println("Thread"+
threadNumber+"("+ countDown +")");

while (true){
try

{
if (myarray[0] <= myarray[1] && myarray[1] <= myarray[2] && myarray[2] <= myarray[3]);

Thread.yield();
Thread.sleep(1000);
if (myarray[0] > myarray[1])
{
swap(myarray,myarray[0],myarray[1]);
}
if (myarray[1] > myarray[2])
{
swap(myarray,myarray[1],myarray[2]);
}
if (myarray[2] > myarray[3])
{
swap(myarray,myarray[2],myarray[3]);
}
System.out.println(myarray[0] +" "+ myarray[1] +" "+ myarray[2] +" "+ myarray[3]);
}
catch (InterruptedException e){}
//if (myarray[0] <= myarray[1] && myarray[1] <= myarray[2] && myarray[2] <= myarray[3]);
// {
// return;
// }
if(-- countDown == 0 )
return;
}
}
}
public static void main ( String args[] )
{
for(int i =0; i<4; i++)
{
MultiThread mT = new MultiThread();
Thread t = new Thread(mT);
t.start();
}
System.out.println("All Threads Started");
}

}
[ March 18, 2004: Message edited by: james walder ]
[ March 18, 2004: Message edited by: james walder ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic