Solved

Multithread within applet

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

734 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