Java threading in distributed computing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Java threading in distributed computing

  1. #1
    Join Date
    May 2014
    Posts
    4

    Post Java threading in distributed computing

    Hello ,

    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.

    Regards,
    Mukund Sawant
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,430

    Re: Java threading in distributed computing

    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.
    Norm

  3. #3
    Join Date
    May 2014
    Posts
    4

    Re: Java threading in distributed computing

    I have pasted the code as below ,
    Can you please help with little code which can demonstrate all in one?

    Code:
    import java.lang.*;
     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]; 
    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;
    		 count=count+1;
    		                      }
    	
    	}
    	
                                                    }
    
    
    }
    
    public class prime_test {
        //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;
            for (t=0; t<nThreads; t++)
            {
    		System.out.println("start:- " + " " +start1);
    		System.out.println("end:- " + " " +end1);
    		//prime2 p[t]=new prime2();
    		//pthreads[t].call(start1,end1);
                pthreads[t] = new prime2(start1,end1);
                pthreads[t].start();
    			start1=end1+1;
    			if(end1 <= max)
    			{
    			end1=end1+coun;
    			}
    			else
    			{
    			end1=max;
    			}
    			
            }
    
            for (t=0; t<nThreads; t++)
            {
                pthreads[t].join();
              //  System.out.println("Thread "+t
               //         +"  Prime count: "+ pthreads[t].count);
    					
    					for (int hk=0;hk<(pthreads[t].vn);hk++)
    					{
    					if(pthreads[t].primes[hk] != 0)
    					{
    					                      
    											 }
    	
    					}
            }
            
            for (int i=0;i<nThreads; i++)
                System.out.println(""+i+": "+pthreads[i].primes[i]);
            endtime = System.currentTimeMillis();
            runtime = endtime - starttime;
            System.out.println("The run time is " +runtime +" milliseconds");
    
        }
    
        }
    Code:
    import java.net.*;
    import java.io.*;
    
    public class EchoClient 
    {
      public static void main( String[] args )
      {
       try
       {
    	 Socket s = new Socket( "localhost", 2000 );
    	 BufferedReader br = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
    	 PrintStream ps = new PrintStream( s.getOutputStream() );
    	 BufferedReader sysin = new BufferedReader( new 
    	 InputStreamReader( System.in ) );
    	 String line = sysin.readLine();
    	 while( line.length() > 0 )
    	 {
    	  ps.println( line );
    	  ps.flush();
    	  String serverResponse = br.readLine();
    	  System.out.println( "Server: " + serverResponse );
    	  line = sysin.readLine();
    	 }
    	 s.close();o
       }
       catch( Exception e )
       {
    	 e.printStackTrace();
       }
      }
    }
    Code:
    import java.net.*;
    import java.io.*;
    
    public class EchoServer {
      private ServerSocket serverSocket;
    
      public EchoServer()  {
       try {
    	 serverSocket = new ServerSocket( 2000 );
    	 System.out.println( "Started Echo Server on port 2000" );
    	 while( true ) {
    	  Socket s = serverSocket.accept();
    	  System.out.println( "Received a new connection, creating the thread to handle it" );
    	  SocketHandlerThread thread = new SocketHandlerThread( s );
    	  thread.start();
    	 }
       }
       catch( Exception e ) {
    	 e.printStackTrace();
       }
      }
    
      public static void main( String[] args ) {
       EchoServer server = new EchoServer();
      }
    
      class SocketHandlerThread extends Thread {
       private Socket s;
    
       public SocketHandlerThread( Socket s ) {
    	 this.s = s;
       }
    
       public void run() {
    	 try {
    	  BufferedReader br = new BufferedReader( new 
    	  InputStreamReader( s.getInputStream() ) );
    	  PrintStream ps = new PrintStream( s.getOutputStream() );
    	  String line = br.readLine();
    	  while( line != null ) {
    	    System.out.println( "Received from client: " + line );
    	    ps.println( line );
    	    ps.flush();
    	    line = br.readLine();
    	  }
    	  s.close();
    	  System.out.println( "Closed Socket..." );
    	 }
    	 catch( Exception e ){
    	  e.printStackTrace();
    	 }
       }
      }
    }

  4. #4
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,430

    Re: Java threading in distributed computing

    The posted code has compiler errors. Please correct them.

    The code that computes the values needs a way to return its results.

    Can you explain the steps a user would take to execute the above code for testing?
    Last edited by Norm; May 10th, 2014 at 08:29 AM.
    Norm

  5. #5
    Join Date
    May 2014
    Posts
    4

    Re: Java threading in distributed computing

    Hello Norm,

    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");
    
        }
    
        }

    Regards,
    Mukund

  6. #6
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,430

    Re: Java threading in distributed computing

    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.
    Norm

  7. #7
    Join Date
    May 2014
    Posts
    4

    Re: Java threading in distributed computing

    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?

    Thank you Norm...

    Regards,
    Mukund

  8. #8
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,430

    Re: Java threading in distributed computing

    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.
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center