Tech2 Forums - Powered by vBulletin

Simple thread implementation in Java ...

This is a topic on Simple thread implementation in Java ... within the Programmers Corner forums, part of the Technology category; Well , i hope ur aware of the tern "Threads" , and "Threading" . if so , then carry on ...

Results 1 to 2 of 2
  1. #1
    Newbie
    Join Date
    May 2005
    Location
    Barrackpore, India
    Posts
    68
    Liked
    0 times
    Reputation points
    0

    Simple thread implementation in Java ...

    Well , i hope ur aware of the tern "Threads" , and "Threading" . if so , then carry on . if not then refer to

    http://java.sun.com/docs/books/tutorial ... ncurrency/

    Here is a simple program to implement the trick :

    Code:
    class thread1 extends Thread{
    	public void run(){
    		for(int i=0;i<10;i++)
    			System.out.println("From Thread 1 -->> "+i);
    	}
    }
    class thread2 extends Thread{
    	public void run(){
    		for(int i=0;i<10;i++)
    			System.out.println("From Thread 2 -->> "+i);
    	}
    }
    class threads{
    	public static void main(String args[]){
    		thread1 th1=new thread1();
    		thread2 th2=new thread2();
    		th1.start();th2.start();
    	}
    }
    Run the above code more than twice to see the output . u should get a different output almost everytime . increase the no. of threads to get even more varied output .

    And . for a practical implementation . here u go . a search program using threads :

    Code:
    import java.io.*;
    
    class threads extends Thread{
        int n;
        threads (int n) {
            this.n = n;
        }
        public void run(){
            if(mainclass.find==n)
    			mainclass.position=mainclass.index+1;
    		mainclass.index++;
    	}
    }
    class mainclass{
    	static int arr[]=new int[200];
    	static int find=0,index=0,position=-1,total=0;
    
    	public static void main(String args[])throws IOException{
    		DataInputStream di=new DataInputStream(System.in);
    		System.out.print("Enter total number of elements : ");
    		mainclass.total=Integer.parseInt(di.readLine());
    		threads a[]=new threads[mainclass.total];
    		for(int i=0;i<mainclass.total;i++){
    			System.out.print("Enter element "+(i+1)+" : ");
                a[i]=new threads(new Integer(di.readLine()).intValue());
    		}
    		System.out.print("Enter element to search : ");
    		mainclass.find=new Integer(di.readLine()).intValue();
    		for(int i=0;i<a.length;i++)
    			a[i].start();
    		label :
    		while(mainclass.index<mainclass.total && mainclass.position<0)
    			continue label;
    		if(mainclass.position<0)
    			System.out.println("Element not present.");
    		else
    			System.out.println("Element present at "+(position));
    	}
    }

  2. #2
    Uber Newbie
    Join Date
    Dec 2004
    Posts
    0
    Liked
    0 times
    Reputation points
    0
    Its a good practice to include the join statements at the end of the lines so that the main thread doesn't ends before the clild threads.
    For the program displayed here it should be like

    class threads{
    public static void main(String args[]){
    thread1 th1=new thread1();
    thread2 th2=new thread2();
    th1.start();th2.start();
    th1.join();
    th2.join();

    }
    }

Posting Permissions

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