Results 1 to 8 of 8

Thread: Threads

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843

    Threads

    HELLO THERE...

    what do I have to do in order to have two Threads with each one with their different run method in order to have separete processes??

    oopss...
    "The difference between mad and genius is the success"

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Not quite sure what you mean. Do you mean just create a seperate thread of execution? Either have you class extend the Thread class or have your class implement the Runnable interface and pass that class to the thread constructor. The second option is better because is frees up your class to extend another class if it so wishes.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    Thanks...

    what I mean.. is for example I have class 1... and I want to have two different procceses running there.. is it possible or do I have to create a class for each separate process?

    Hey... Dilenger4....

    let say I have a class that helps me getting the input from the user...
    [code]
    BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
    String files = new String(buff.readLine());
    [/code[

    and each time I create a class like this:
    Code:
      InputClass c=new InputClass();
    and when I run it... I want a new DOS window so I can type data in the different classes....

    you know what i mean?
    "The difference between mad and genius is the success"

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Sorry i had to delete the post. For some reason it was synchronizing No you can do it all in the same process. Seperate classes arent need.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    ohhh...

    humm...could you help me with a small example???

    thanks
    "The difference between mad and genius is the success"

  6. #6
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Here is an Example..... The code just simply takes command line arguements specified as files and searches each file for certain characters. If you notice i used a static inner class to do the actual parsing. Since the inner-class implements the Runnable interface and declares a Thread we have to pass a refrence to the thread object that implements the Runnable interface which happens to be the same class.

    Code:
      import java.io.*;
      import java.util.StringTokenizer;
      
     class Slicer{
        public static void main(String[] args){
      	
      for(int i = 0; i < args.length; i++){
        Test t = new Test(args[i]);
       }
     }
    
     static class Test implements Runnable{
         private String file; 
         private Thread t; 
      	
        public Test(String file){
           this.file = file;
           t = new Thread(this);
           t.start(); 
        }		
      	
        public synchronized void run(){
        int c;
       
        try{	
        File f = new File(file);
          char[] cbuff = new char[(char)f.length()];  
     
         int index = 0; 
      	 
         BufferedReader buff = new BufferedReader(new FileReader(f));
      	
         while((c = buff.read(cbuff)) != -1){}
          buff.close();
       	
        for(int i = 0; i < cbuff.length; i++){
          switch(cbuff[i]){
             case '?':
             System.out.println(" Encountered a ? in file " + f.getName());
             break;
             case '#':	
             System.out.println(" Encountered a # in file " + f.getName());
             break;
             case '$':	
             System.out.println(" Encountered a $ in file " + f.getName());
             break;
         } 		 
        }
       }catch(IOException e){System.out.println(e);}
      } 
     }  	
    }

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    There are also other issues that one has to take into account when multithreading. For a synchronized class method(static method), Java obtains an exclusive lock on the class before executing the method. For a synchronized instance method, Java
    obtains an exclusive lock on the class instance. So it's actually possible(though ive never tried it)to run bolth synchronized instance methods and synchronized class methods at the same time.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    Sorry for not answering before....

    so.. that example seems very clear. You create a class for each Process you want. ok... I have two questions

    1. What about if I want different code in the run method for the thread... (I can of know the answer.... USE a if statement or declaring different kind of classes with different run methods?

    2. I didnt understand quite weel what the synchronized word was for. I am going to do some research but if you can help me a little I would cretanly appreciate it.


    THANK you

    "The difference between mad and genius is the success"

Posting Permissions

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



Click Here to Expand Forum to Full Width