I am developing one program to find the prime numbers from 1 to 1000000.
If I run the same program on one computer it is generating the load on one computer.So I decided to to distribute the load with the threading concept on 2 different computers.So what will happen one computer will find the prime numbers from 1 to 1000 , the second one will find 1001 to 2000, again first one will find 2001 to 3000 , again second will find 3001 to 4000 .(...) so on..
How can I achieve this through client-server computing and threading concept?
I am attaching my source code here.
When you start the worker, pass it the range of numbers it is to work in.
Have it return a list of the numbers it found. Save the returned numbers in a list.
Post any code you have questions about here. Be sure to wrap the code in code tags.
Thank you for attention.
I could compile and run the program without any errors.
Echoclient and Echoserver are the programs which demonstrates client-server communication.
But I will explain the steps to run the below program to find the prime number using threading.
javac prime_test1.java
java prime_test1 100
here 10=number of threads ..
here 100000 is the maximum number , upto which we have to find the prime numbers.
means 100 threads will be created ,
Thread1 :- Will find the prime numbers between 2 to 1000
Thread2 :- Will find the prime numbers between 1001 to 2000
Thread3:- Will find the prime numbers between 2001 to 3000
Thread4:- Will find the prime numbers between 3001 to 4000
upto thread 100......
Here I want to get distributed these n threads (here n=100 threads ) over p number of computers(here p=2) .
So load will be distributed across p nodes.
So if thread1 executes on computer1 , then parallely thread2 will be passed on computer2 , then thread3 again on computer1.....
Code:
import java.lang.*;
import java.util.*;
class prime2 extends Thread{
long n=3;
public long vn=0;
public int count=0;
public long thread = 100;
public long[] primes = new long[100000];
ArrayList al=new ArrayList();
long start1;
long end1;
//public static int count=3;
public prime2(long k1,long k2)
{
start1=k1;
end1=k2;
}
/*public void call(long k1,long k2)
{
start1=k1;
end1=k2;
}*/
public void run() {
long myNumber;
for (long nm=start1;nm<=end1;nm++)
{
int flag1=0;
for (long j=2;j<=(nm/2);j++)
{
long res=nm%j;
if(res== 0 )
{
flag1=1;
vn=vn+1;
}
}
if (flag1==0 || nm==2 || nm==3 || nm==5 || nm==7)
{
if(nm>2 && nm<100)
{
//System.out.println(nm);
}
primes[count] = nm;
// al.add(nm);
count=count+1;
}
}
}
}
public class prime_test1 {
//static final int nThreads = 2;
public static void main(String[] args) throws InterruptedException{
int t;
int total = 0;
int nThreads = Integer.parseInt(args[0]);
//prime2 p[nThreads]=new prime2();
prime2[] pthreads = new prime2[nThreads];
//*Scanner kb = new Scanner(System.in);
//*System.out.println("Enter a Positive Integer: ");
//*long num = kb.nextLong();
long starttime, endtime, runtime, a = 0;
starttime = System.currentTimeMillis();
long max = 100000;
long start1=2;
long end1=(max/nThreads);
long coun=end1;
long diff1;
System.out.println();
System.out.println("--- " + nThreads + " threads with " + end1 + " partitions---");
System.out.println();
for (t=0; t<nThreads; t++)
{
if(t==0)
{
System.out.println("-----------------------------------------");
}
System.out.println("start:- " + " " +start1);
System.out.println("end:- " + " " +end1);
System.out.println("-----------------------------------------");
//prime2 p[t]=new prime2();
//pthreads[t].call(start1,end1);
pthreads[t] = new prime2(start1,end1);
pthreads[t].start();
start1=end1+1;
end1=end1+coun;
diff1=100000-end1;
if(diff1 <coun)
{
end1=max;
}
}
for (t=0; t<nThreads; t++)
{
pthreads[t].join();
// System.out.println("Thread "+t
// +" Prime count: "+ pthreads[t].count);
}
for (int i=0;i<nThreads; i++)
System.out.println(""+i+": "+pthreads[i].count);
endtime = System.currentTimeMillis();
runtime = endtime - starttime;
System.out.println("The run time is " +runtime +" milliseconds");
}
}
I suggest that you start without attempting threads. Change the server and client so that the when the client connects to the server, the server sends the client a range to generate, the client generates the numbers in that range, returns the list of numbers to the server and waits for the next instruction: another range or an end of task message.
When that is working, then work on using threads on the server to control multiple clients.
I don't think this is a need to have more than one thread per client.
Here I have to perform workload balancing between p nodes and at the same time load should be distributed.If I do the same thing without threads then I think it is not possible.Is it possible?
Or can you please show me the way by the small code?
I suggest that you build the program one small step at a time. First get the client / server communications working
using a single client that is called repeatedly.
When that works, change the code to have more than one client working using threads.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.