Solved

Multithread within applet

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

757 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

18 Experts available now in Live!

Get 1:1 Help Now