• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Updated MulitThreads Problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic