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

x
?
Solved

Multithread within applet

Posted on 2000-05-03
4
Medium Priority
?
385 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
[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 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

730 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