The Artima Developer Community
Sponsored Link

Java Answers Forum
palindrome numbers with threads in java

1 reply. Most recent reply: Nov 10, 2007 2:41 PM by j j

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a flat view of this topic  Flat View
Previous Topic   Next Topic
Threaded View: This topic has 1 reply on 1 page
j j

Posts: 2
Nickname: jopeter
Registered: Nov, 2007

palindrome numbers with threads in java Posted: Nov 10, 2007 10:40 AM
Reply to this message Reply
hi
i have wrote this progaram in java.
it finf the palidrome numbers and print it.
its by threads

it has some errors and i can t run it.
please help me.

package palindromen;
import java.lang.Integer;
import java.lang.String;

public class Main {

public Main() {
}

public static void main(String[] args)
{int nop,anyt;
Keyboard key=new Keyboard();
System.out.println("Enter number of processors : ");

nop=key.readInt();
anyt=20000/nop;

T Threads[]=new T[nop];
Palindromeset p=new Palindromeset();
for(int i=1;i<=nop;i++) {
Threads=new T("Threads"+Integer.toString(i));
Threads.setr((i*anyt),anyt*(i+1),p);

}

for (int i=1;i<=nop;i++)
Threads.start();

for(int i=1;i<=nop;i++)
try{
Threads.join();
}
catch(Exception e)
{ }



p.print();





}

}

-------------------------------------

package palindromen;


public class Palindromeset extends Thread{
private int buffer[];
private int count;

/** Creates a new instance of Palindromeset */
public Palindromeset() {
count=0;
buffer=new int[20000];
}


public synchronized void addNum(int x)
{buffer[count]=x;
count++;
}

public void print(){
for(int i=0;i<count;i++)
{ System.out.println(buffer);
System.out.println(" ");
}
System.out.println();
System.out.println(count);

}



}
---------------- ---------------------------------

package palindromen;


public class T extends Thread{


public int lowr,highr,n;

volatile Palindromeset c;


public void setr(int lr,int hr,Palindromeset pt)
{
lowr=lr;
highr=hr;
c=pt;}


public T(String s) {

this.setName(s);
}

public void run()

{for(int i=lowr;i<highr;i++){


n = i; //used at last time check

int reverse=0,remainder;

while(i > 0){

remainder = i % 10;

reverse = reverse * 10 + remainder;

i = i / 10; }

if(reverse == n)
c.addNum(i);
}
}
}


Re: palindrome numbers with threads in java Posted: Nov 10, 2007 2:41 PM
Reply to this message Reply
Posted by: j j    Posts: 2 / Nickname: jopeter / Registered: Nov, 2007
i completed my program by tags
i hope u help me.




package palindromen;
import java.lang.Integer;
import java.lang.String;

public class Main {

public Main() {
}

public static void main(String[] args)
{//nop is number of processors.we inter it via keyboard
//for example we have number 0 - 20000.we want to know nubber of palindrome numbers between0-20000
// anyt=20000/nop; 20000/nop is the number of number that every thread should start
int nop,anyt;
Keyboard key=new Keyboard();
System.out.println("Enter number of processors : ");

nop=key.readInt();
anyt=20000/nop;

T Threads[]=new T[nop];
Palindromeset p=new Palindromeset();
for(int i=1;i<=nop;i++) {
//here we say to every thread from what number to what number it should define the palindrome numbers
//for exmaple if we have 10 processors ,Theard[0] should define palindrom numbers 0 to 2000
Threads=new T("Threads"+Integer.toString(i));
Threads.setr((i*anyt),anyt*(i+1),p);

}

for(int i=1;i<=nop;i++)
Threads.start();

for(int i=1;i<=nop;i++)
try{
Threads.join();
}
catch(Exception e)
{ }



p.print();//caling print() from class Palindromeset





}

}
---------------------------------------------------------

package palindromen;


public class T extends Thread{


public int lowr,highr,n;

volatile Palindromeset c;


public void setr(int lr,int hr,Palindromeset pt)
{
lowr=lr;//lowrang
highr=hr;//highrang
c=pt;}


public T(String s) {

this.setName(s);
}

public void run()

{for(int i=lowr;i<highr;i++){


n = i; //used at last time check

int reverse=0,remainder;

while(i > 0){

remainder = i % 10;

reverse = reverse * 10 + remainder;

i = i / 10; }

if(reverse == n)
/*in the number is palindrome we add count bc we want to khow the number of palindrom numbers
and we put the palindrome number in array bubber bc at last we want to print the
palindrom numbers*/
c.addNum(i); //error: at palindromen.T.run(T.java:44)

}
}
}
--------------------------------------------------------------

package palindromen;


public class Palindromeset{
private int buffer[];
private int count;

/** Creates a new instance of Palindromeset */
public Palindromeset() {
count=0;
buffer=new int[20000];
}


public synchronized void addNum(int x)
{buffer[count]=x;
/*error : Exception in thread "Threads0" java.lang.ArrayIndexOutOfBoundsException: 15
at palindromen.Palindromeset.addnum(Palindromeset.java:17)*/

count++;
}

public void print(){
for(int i=0;i<count;i++)
{ System.out.println(buffer);//print the palindrome numbers that exist in buffer[count]
System.out.println(" ");
}
System.out.println();
System.out.println(count); //print number of palindrome numbers

}



}


Topic: palindrome numbers with threads in java Previous Topic   Next Topic Topic: Worst Code ever!  Restrictive Swing Frameworks

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use