Results 1 to 2 of 2
  1. #1
    pricelessjunk is offline Member
    Join Date
    Aug 2009
    Posts
    1
    Rep Power
    0

    Default Applet and multithreading

    hey guyz...am really having trouble with this prog...can anyone plzzz help....i'll explain the code...

    I have a simple applet code..it marquees a line...i want to make it stop at a particular interval and make it continue on mouse clicking...so I use wait()..but on using notifyAll() nothing happens...can anyone plz help...and forgive me if there is any stupid errors in the code...


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    
    public class threadtest extends Applet implements MouseListener{
    
    String msg,statusmsg;
    int x=20,y=30,i;
    newthread nt=new newthread();
    
    public void init()
    {
    setForeground(Color.blue);
    addMouseListener(this);
    }
    
    public void paint(Graphics g)
    {
    g.drawString(msg, x, y);
    
    }
    
    public void mouseExited(MouseEvent me){}
    public void mouseEntered(MouseEvent me){}
    public void mousePressed(MouseEvent me){}
    public void mouseReleased(MouseEvent me){}
    
    public void mouseClicked(MouseEvent me)
    {
    showStatus("Mouse is clicked");
    msg="look at the mouse";
    repaint();
    nt.t.start();
    }
    
    synchronized void resumenow()
    {
    notify();
    }
    
    synchronized void suspendnow()
    {
        try
        {
             showStatus("Thread is in suspendnow()");
             wait();
        }
        catch (InterruptedException ex) {}
    }
    
    
    
    public void start()
    {
    msg = "Started";
    
    
    for(i=0;i<100;i++)
    {
        if(x>200)
        {
           x=20;
        }
        x=x+1;
        repaint();
        if(i%3==0)
        {
             showStatus("This thread is gonna wait");
             repaint();
             suspendnow();
            showStatus("thread is waiting");
    
          }
    }
    
    }
    }
    
    
    
    class newthread implements Runnable{
    
    Thread t;
    
    public newthread()
    {
    t=new Thread(this);
    }
    
    public void run()
    {
    resumenow();
    }
    
    synchronized void resumenow()
    {
    notifyAll();
    }
    
    synchronized void suspendnow()
    {
        try
        {
            wait();
        }
        catch (InterruptedException ex) {}
    }
    
    }

    thanks in advance

  2. #2
    vkorenev is offline Member
    Join Date
    Aug 2009
    Location
    Kharkov, Ukraine
    Posts
    29
    Rep Power
    0

Similar Threads

  1. multithreading
    By shilpa.krishna in forum New To Java
    Replies: 2
    Last Post: 06-27-2008, 04:18 PM
  2. Replies: 1
    Last Post: 11-16-2007, 10:09 AM
  3. Multithreading + Networking (desperate)
    By bluebarca in forum New To Java
    Replies: 1
    Last Post: 11-07-2007, 02:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •