aspose file tools*
The moose likes Threads and Synchronization and the fly likes Updated MulitThreads Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Updated MulitThreads Problem " Watch "Updated MulitThreads Problem " New topic
Author

Updated MulitThreads Problem

james walder
Greenhorn

Joined: Mar 17, 2004
Posts: 1
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
 
subject: Updated MulitThreads Problem