Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use Thread

Posted on 2003-11-20
10
Medium Priority
?
329 Views
Last Modified: 2010-03-31
in my app, i have 3 methods, i wanted them to run together..

public class APP{

  public APP{
    doThis();
   doThat();
   doIt();
  }

  void doThis(){//some code}
  void doThat(){//some code}
  viod doIt(){//some code}
}

so, how am i suppose to make all this three method run at the same time using Thread?
0
Comment
Question by:zeBes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 3

Accepted Solution

by:
applekanna earned 100 total points
ID: 9794714
write your three mtehods in three differnt class as follows
public class ThreadDemo1 extends Thread {

public ThreadDemo1() {
}
public void run() { //this is the importnat method of any class that extends thread

//doThis() code here
}
}

same way doit for ThreadDemo2 , ThreadDemo3
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9794718
This is the main prg

public final class ThreadDemoTester {
public static void main(String[] args) {
ThreadDemo1 demo1 = new ThreadDemo1();
ThreadDemo2 demo2 = new ThreadDemo2();
ThreadDemo3 demo3 = new ThreadDemo3();

demo1.start();
demo2.start();
demo3.start();
}
}

Now all three thread will be runing.

Hope this helps
Cheers!

0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 15

Expert Comment

by:Javatm
ID: 9794742
You could study this :
http://java.sun.com/docs/books/tutorial/essential/threads/index.html

You could also try this :

1st Program :

public class Producer extends Thread {
    private CubbyHole cubbyhole;
    private int number;

    public Producer(CubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            cubbyhole.put(i);
            System.out.println("Producer #" + this.number
                               + " put: " + i);
            try {
                sleep((int)(Math.random() * 100));
            } catch (InterruptedException e) { }
        }
    }
}

-----------------------------------------------------------

2nd Program :

public class Consumer extends Thread {
    private CubbyHole cubbyhole;
    private int number;

    public Consumer(CubbyHole c, int number) {
        cubbyhole = c;
        this.number = number;
    }

    public void run() {
        int value = 0;
        for (int i = 0; i < 10; i++) {
            value = cubbyhole.get();
            System.out.println("Consumer #" + this.number
                               + " got: " + value);
        }
    }
}

-----------------------------------------------------------

Main Program :

public class ProducerConsumerTest {
    public static void main(String[] args) {
        CubbyHole c = new CubbyHole();
        Producer p1 = new Producer(c, 1);
        Consumer c1 = new Consumer(c, 1);

        p1.start();
        c1.start();
    }
}

Hope it helps . . .
0
 

Author Comment

by:zeBes
ID: 9795567
must write another class for to use Thread?
can i write internal class and extends Thread? or implement Runnable?

pts = 50
0
 

Author Comment

by:zeBes
ID: 9795616
sorry, forget to add.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9795939
You can, but be careful: innerclasses can't easily access the data around.

You might extend your own class from Thread, but .... how is "run()" to know that it must do something else
than the previous Thread you started or the next one: run() takes no parameters.

It is very difficult to write a single Thread class that can find out what to do, not conflicting with its brothers and sisters.
In general we write a Thread class with a clear name so everybody understands its special purpose.
;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9796455
If the inner classes are not anonymous, then they have access to the data of the outer class, so you can do this, although you must beware of synchronization on variables:

public class App {
      int i = 10;

      public App() {
            MethodExecutor m1 = new MethodExecutor() {
                  public void run() {
                        doThis();
                  }
            };
            MethodExecutor m2 = new MethodExecutor() {
                  public void run() {
                        doThat();
                  }
            };
            MethodExecutor m3 = new MethodExecutor() {
                  public void run() {
                        doIt();
                  }
            };
            m1.start();
            m2.start();
            m3.start();
      }

      void doThis() { System.out.println("doThis() called. i = " + i); }
      void doThat() { System.out.println("doThat() called. i = " + i); }
      void doIt() { System.out.println("doIt() called. i = " + i); }



      class MethodExecutor extends Thread {
            public void run() {
            }
      }

      public static void main(String[] args) {
            new App();
      }
}
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 100 total points
ID: 9800644
1.) Must write another class for to use Thread?

- Yes much better and extendable !

2.) Can i write internal class and extends Thread? or implement Runnable?

- Yes you can but be carefull as " sciuriware " said can't easily access the data around.

   Also CEHJ said : although you must beware of synchronization on variables:

It's up to you on what to use.

Hope it helps . . .
JAVATM

0
 
LVL 3

Expert Comment

by:applekanna
ID: 9836439
Thx for the points :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question