This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updated MulitThreads Problem

 
james walder
Greenhorn
Posts: 1
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic