11 Replies Latest reply on Sep 21, 2004 9:55 AM by 807596

    Threads

    807596
      Hi,
      I have class counter:
      public class Counter {
           
           int count=0;
           MyThread a = new MyThread(50,null);
           Counter (){};
           
           
      public void incCount( ){
           
                for(int i=0; i<=10; i++) {
                     System.out.println(i + " ");
                     count+=count;
           
                }
           try{
           a.wait();}
           catch (InterruptedException e){};
      }


      public void resetCount( ){
           count = 0;
           a.notify();
           
           
      }
      public static void main(String[] args){
           Counter counter = new Counter();
           counter.incCount();
           
      }

      }

      MyRunnable:
      public class MyRunnable implements Runnable{
           Object bu;
           
                MyRunnable(Object j){
                bu=j;}
           
           
                public void run(){
                     try{
                     Thread.sleep(2000);
                new Counter().resetCount();
                     }
                     catch (InterruptedException e){};
                }

                
                }

      main:

      public class ThreadTestDriver{

      public static void main(String[] args){
                Counter counter = new Counter();
                MyRunnable myrunnable =new MyRunnable(counter);
                MyThread mythread = new MyThread(50,myrunnable);
                mythread.start();
                new Thread(myrunnable).start();
           }


      }

      why it gives error when I start: java.lang.Error: Unresolved compilation problem:
           The constructor Thread(MyRunnable1) is undefined

           at ThreadTestDriver.main(ThreadTestDriver.java:8)
      Exception in thread "main" ?

      Thanks.
        • 1. Re: Threads
          807596
          Try
          new Thread((Runnable)myrunnable).start();
          ... :)
          • 2. Re: Threads
            807596
            The constructor Thread(Runnable) is undefined :)
            • 3. Re: Threads
              796440
              The constructor Thread(Runnable) is undefined :)
              You're mistaken in that:

              [url http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable)]Thread(Runnable)
              • 4. Re: Threads
                807596
                but it's written:

                The following code would then create a thread and start it running:


                PrimeRun p = new PrimeRun(143);
                new Thread(p).start();
                Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.

                • 5. Re: Threads
                  807596
                  The constructor Thread(MyRunnable1) is undefined
                  at ThreadTestDriver.main(ThreadTestDriver.java:8)
                  Exception in thread "main" ?
                  Not to lose the plot but this compile error is TELLING you that the value supplied to the Thread
                  constructor DOES NOT CONFORM to any Thread constructor...

                  While this is strange, implicitly CASTING it to Runnable should bypass this warning...

                  What is the babble about names.... ?
                  • 6. Re: Threads
                    807596
                    But if I try to Cast it like: new Thread((Runnable)myrunnable).start();
                    it still gives : The constructor Thread(MyRunnable) is undefined.
                    • 7. Re: Threads
                      807596
                      <stumped!>

                      What IDE, if any , are you using... also what ver JDK?
                      • 8. Re: Threads
                        807596
                        eclipse, j2sdk1.4.2
                        • 9. Re: Threads
                          807596
                          Check the following:
                          public class Rabbit
                          {
                              public Rabbit()
                              {
                                 System.out.println("weeee..");
                              }
                              public static void main(String[] args)
                              {
                                    new Thread((Runnable)new Rabbit()).start();
                              }
                          }
                          Now... using javac and netbeans, the above compiles......

                          Check out the eclipse fora maybe for an answer......
                          • 10. Re: Threads
                            807596
                            I got this:

                            java.lang.ClassCastException: Rabbit
                                 at Rabbit.main(Rabbit.java:5)
                            weeee..
                            Exception in thread "main"
                            • 11. Re: Threads
                              807596
                              I would have been shocked if you had got anything else... OF course it will throw a
                              cce... Rabbit never implemented Runnable...!

                              This was to illustrate to you that the problem may be with your compiler - by using an explicit
                              cast the compiler assumes that the object is that which we have cast it too.. until runtime at which point
                              it all comes crashing down around your ears....

                              I never expected you tto try and compile this, btw... considering that it is nonsense code... :)

                              I suspect that if even the cast is throwing a compile error, check for assistance on eclipse....