Results 1 to 14 of 14

Thread: running threads [resolved]

  1. #1

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    running threads [resolved]

    I've look at all the API docs and tutorials I can find and I still don't know how to start a thred.

    I know (correct me if I'm wrong please) that to create something that can be used as a thread it must either extend Thread or implement Runnable and then override the run() method with the code that you would normally put in a main....
    Last edited by CaptainPinko; Oct 22nd, 2001 at 10:30 AM.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Here's a simple thread example:
    Code:
    class Athread implements Runnable
    {
    
        void run()
        {
            for(int x=0;x<10;x++)
            {
                System.out.println(x);
                Thread.sleep(200);
            }
        }
    
    }
    
    class Aprog
    {
    
        Thread t = new Athread();
    
        t.start();
    
    }
    that should work, but I haven't used threads in a few weeks, so it may be buggy.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Here is a little sample code for you to look at. There are a couple of ways to multithread in Java. You can either subclass thread
    or declare a class that implemlemts a Runnable interface and pass an instance of that class to the Threads constructor.
    If you want to multithread in the same class you could do somthing like this.
    Code:
    public class ThreadTest implements Runnable{
      
     private Thread worker; 
     public ThreadTest(){ 
        worker = new Thread(this, threadname)
        worker.start();  
     } 
      
      public void run(){
       // code...............
      }
     }
    It's normally perfered to not subclass the Thread class because the subclass will not be able to extends other classes so this is somthing to consider.
    Some sample code............

    Code:
    class Storage{
       int value; 
       synchronized void setValue(int i){value = i;}
       synchronized int getValue(){return value;}      
     }
      
      class Printer implements Runnable{
        Thread t1;
        Storage store; 
        public Printer(Storage target){
         store = target; 
          t1 =  new Thread(this);
          t1.start(); 
        }
    
       public void run(){
        try{
        while(true){
         System.out.println("The value of storage is" + store.getValue());
         t1.sleep(1000); // 1 second of sleep
        }
       }catch(Exception e){System.err.println(e);}
        }
      }
     
     class Counter implements Runnable{
       Thread t2;  
       int i;  
       Storage storage; 
       public Counter(Storage target){
        storage = target;  
         t2 = new Thread(this);
         t2.start(); 
       }
        public void run(){
         try{ 
          while(true){
            storage.setValue(i);
             ++i;  
             t2.sleep(1000); // 1 second of sleep
          }
        }catch(Exception e){System.err.println(e);}
        }  
      }       
    
     public class Client{
       public static void main(String[] args){
         Storage store = new Storage(); 
         new Counter(store);
         new Printer(store);  
       }
     }

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Did you have any luck?

  5. #5

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332
    CrptcBlade your example doesn't work, i tried this
    Code:
    public class ReadTester {
        public static void main (String args[]) {      
            Thread a = new BigMe();
        }
    }
    class BigMe implements Runnable {
    	public void run () {for (int i = 0; i < 10; i++) System.out.println ("Big Me");}	
    }
    and got the JAVAC error:

    D:\Java\ReadTester.java:3: incompatible types
    found : BigMe
    required: java.lang.Thread
    Thread a = new BigMe();
    ^
    and Dilenger4, your example is quite cluttered and i can't really see what you are doing
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  6. #6
    Originally posted by crptcblade
    Here's a simple thread example:
    Code:
    class Athread implements Runnable
    {
    
        void run()
        {
            for(int x=0;x<10;x++)
            {
                System.out.println(x);
                Thread.sleep(200);
            }
        }
    
    }
    
    class Aprog
    {
    
        Thread t = new Athread();
    
        t.start();
    
    }
    that should work, but I haven't used threads in a few weeks, so it may be buggy.

    Two classes in one .java file = big no-no.

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by filburt1


    Two classes in one .java file = big no-no.
    filburt1 = big do-do

    You can have more than one class in one java file. The compiler will separate them and create different .class file for each class.

    And, my example did have a couple of things wrong with it, now that I am looking at it again, I will see if I can rectify them in a bit.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Thumbs down

    filburt1, God of cut'n'paste, that code DOESN"T work as I said so *** was the purpose of posting it again?
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  9. #9
    I was just quoting him.

  10. #10

    Thread Starter
    Hyperactive Member CaptainPinko's Avatar
    Join Date
    Jan 2001
    Location
    London, Ontario, Canada
    Posts
    332

    Angry

    <smacks head into monitor>for what purpose? to increase peoples' pleasure of scrolling?!?
    Last edited by CaptainPinko; Oct 13th, 2001 at 07:18 PM.
    "There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein

    If you are programming in Java use www.NetBeans.org

  11. #11
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    working example :
    Code:
    public class TestThreads 
    {
    
        public static void main(String[] args)
        {
    		
            Thread t = new Thread(new ThreadTest());
    
            t.start();
        }
    
    }
    
    class ThreadTest implements Runnable
    {
    
        public void run()
        {
            for (int x=0; x<10; x++)
            {
                System.out.println(x);
                try
                {
                    Thread.sleep(500);
                }
                catch(InterruptedException e){}
            }
        }
    
    }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  12. #12
    PowerPoster sail3005's Avatar
    Join Date
    Oct 2000
    Location
    Chicago, IL, USA
    Posts
    2,340
    Originally posted by filburt1


    Two classes in one .java file = big no-no.
    there is nothing wrong with doing that.

    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
    USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA

  13. #13
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Hey, at least my code runs.

  14. #14
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The code i posted was to show what
    happens when synchronization is used
    while in the context of "multi thredding."
    The code really isnt that hard to understand.
    You have two classes that implement a runnable
    interface, which inturn overide the run() method.


    Here you are creating a instance of the Storage
    class which is stored as a refrence in store.
    For Counter and Printer you are creating two
    instances but your just not storing them within
    a refrence variable.

    Code:
    public class Client{
       public static void main(String[] args){
         Storage store = new Storage(); 
         new Counter(store);
         new Printer(store);  
       }
     }
    All this class is doing is setting the value
    of the integer variable "i" in the Storage class.
    one of the Thread class constructors is public
    Thread(Runnable target) Since you are creating
    a thread within the same class that
    is implementing the Runnable interface you just
    pass in "this" to the Thread constructor, as we have done.

    Code:
    class Counter implements Runnable{
       Thread t2;  
       int i;  
       Storage storage; 
       public Counter(Storage target){
        storage = target;  
         t2 = new Thread(this);
         t2.start(); 
       }
        public void run(){
         try{ 
          while(true){
            storage.setValue(i);
             ++i;  
             t2.sleep(1000); // 1 second of sleep
          }
        }catch(Exception e){System.err.println(e);}
        }  
      }
    All this code is doing is printing out the values
    that have been previously set in the Storage class.
    This class is similar to the Counter class.

    Code:
    class Printer implements Runnable{
        Thread t1;
        Storage store; 
        public Printer(Storage target){
         store = target; 
          t1 =  new Thread(this);
          t1.start(); 
        }
    
       public void run(){
        try{
        while(true){
         System.out.println("The value of storage is" + store.getValue());
         t1.sleep(1000); // 1 second of sleep
        }
       }catch(Exception e){System.err.println(e);}
        }
      }
    The Storage class is quite simplistic but it
    servers to illustrate it's point quite nicely.
    All this class contains is a integer variable
    and two accessor methods. That's it.......

    Code:
    class Storage{
       int value; 
       synchronized void setValue(int i){value = i;}
       synchronized int getValue(){return value;}      
     }

    I think what is throwing you off is the use of
    aggregation rather than inheritance.

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