Threads in Java

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Threads in Java

  1. #1
    Join Date
    Feb 2003

    Question Threads in Java

    If I created a subclass called testThread, put a counter in it that increments every 100 milliseconds,
    and add a public method to retrieve the counter value, would the following be correct?

    class clockThread extends Thread {

    int counter = 0;

    public clockThread(int number) {

    while(true) {

    counter = number;
    try {
    catch ( InterruptedException e) {}
    counter ++


    How would I create an applet that has two instances of testThread and uses the paint method to
    display the counters in two threads? How would I adjust the priorities of the two threads?

    Your help in understanding this will be appreciated.

  2. #2
    Join Date
    Nov 2002
    your on the right track already. i would suggest you make a method that returns the value of the counter . this way it can me more accessible since you would want to paint the counter values in an applet. since your class extends Thread, you should have a run() method. i think you should restructure your class clockThread. don't place everything in the constructor. make other methods too.

    after you have defined your class you just do, for example, clockThread ct = new clockThread(.....). of course you can make another instance of this class if you need more than 2. you can set thread priorities by calling the setPriority() method. so ct.setPriority( 1-10 ). (or is it 0-10?) anyway. all threads, i think, are at priority 5 as default. the lower the number the greater the priority.

    do the painting in your applet class. i am assuming you know how to go about this.

  3. #3
    Join Date
    Feb 2003


    Thanks for you help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center