The Artima Developer Community
Sponsored Link

Java Answers Forum
Understanding of Threads in Java

1 reply on 1 page. Most recent reply: Mar 10, 2003 2:31 AM by Alex S

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Keith Cavanaugh

Posts: 1
Nickname: vanuatu
Registered: Feb, 2003

Understanding of Threads in Java Posted: Feb 18, 2003 2:07 PM
Reply to this message Reply
Advertisement
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 {
Thread.sleep(100);
}
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.


Alex S

Posts: 27
Nickname: andu
Registered: Mar, 2003

Re: Understanding of Threads in Java Posted: Mar 10, 2003 2:31 AM
Reply to this message Reply
Every thread starts when you call start() method. This method will call run() method.

So, you have to override the Thread's run() method, put there your thread functionality and then call start() method to start the thread. By the way, if you put code like "while(true)..." in your constructor, I guess your object will never be created.

Pay attention to syncronization if you will share resources (variables, objects etc) between two or more threads.

If you want to learn java threads, read Thread and Runnable java api documentation and thread tutorial from java tutorial (check on http://java.sun.com site).

Flat View: This topic has 1 reply on 1 page
Topic: java.mail imap critical problems Previous Topic   Next Topic Topic: Problem deploying WAR file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use