Solved

Multithread within applet

Posted on 2000-05-03
4
354 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
How can I do a Massively massive community reviewing-and-grading site? 7 69
Cipher Configuration on Apache HTTPD 4 50
rhino JavaScript import, load 25 66
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now