Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multithread within applet

Posted on 2000-05-03
4
Medium Priority
?
388 Views
Last Modified: 2008-07-03
I've created an applet that uses the following code:

public void start() {
      if (oddsThread == null) {
            oddsThread = new Thread(this, "Odds");
            oddsThread.start();
      }
}


public void stop() {
      oddsThread = null;
}

public void run() {
      Thread myThread = Thread.currentThread();
      while (oddsThread == myThread) {
            getOdds();
            try {
                  Thread.sleep(1000*Integer.parseInt(getParameter("UpdatePeriod")));
            } catch (InterruptedException e){}
      }
      oddsThread=null;
}

I also need another thread that is a "clock" and keeps the current time (according to the server).

1. How can I create another thread that keeps track of time (ie. give it a starttime param and then it adds a second every sleep(1000)
2. Alternatively, can I get the "Server" time as opposed to the system (client) time and use this as the clock? The idea is that as soon as the server gets to a certain time the applet should stop udating itself

Cheers

Dave
0
Comment
Question by:deal051298
4 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2776992
>>Thread myThread = Thread.currentThread();
>>while (oddsThread == myThread) {

Why not simply "while (oddsThread!=null)" ?? :-)

What do you mean by "the server"? Are you connecting to a server via a socket? In that case the only way to know what time is it on the server is to ask it... i.e. send it a request about its time.

A thread that counts time:


public void run(){
  while(!stop){
    try{
      Thread.sleep(1000);
    } catch (InterruptedException e){
         return;
       }
    time += 1;
  }
}


This seems to simple meaning that I just probably didn't understand what you mean :-) Please explain further.
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2777417
Assume that three threads thread1,thread2,thread3

And use the same polling

get System's current thread and check the thread name and run the
appropriate code.

Note that only one run() should be there.

U can get server time from applet by using URLConnection to a servlet running on server

visit

http://www.j-nine.com/pubs/applet2servlet/   for that purpose
0
 

Author Comment

by:deal051298
ID: 2778874
Can you please post some code that shows how to start two threads - one that does what my existing code does and one that counts time. Sasha - I don't see how you code would fir into my existing code. Ravindra - could you please post example?
0
 
LVL 3

Accepted Solution

by:
falter earned 300 total points
ID: 2779080
deal,

simply create a Class extending Thread
or implementing Runnable
Add this class as a member to your Applet class.
Construct it and start the new thread.

public class TimeCounter extends Thread
{
public void run(){
// do your timer stuff
}
}

public class YourApplet extends Applet implements Runnable{
TimeCounter tCount = null;

public void start() {
  if (tCount == null) {
      tCount = new TimeCounter();
      tCount.start();
  }
// your code
   ....
   
  }

....

Hope this helps a little bit
jf
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

783 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