Thread example in java

 class A implements Runnable
  {
    String name;
    Thread ThreadA;
    A(String Threadname)
    {
        name=Threadname;
        ThreadA=new Thread(this,name);
        System.out.println("New Thread : "+ThreadA);
        ThreadA.start();
    }
    public void run()
    {
          for (int i=1; i<=5;i++)
               {

                   System.out.println(ThreadA.getName()+" : "+ i);
                try
                  {
                    ThreadA.sleep(500);
                }
                catch(InterruptedException e)
                {

                }
               }

     System.out.println("End of child Thread : "+ThreadA.getName());


    }
}


class B implements Runnable
 {
    String name;
    Thread ThreadB;
    B(String Threadname)
    {
        name=Threadname;
        ThreadB=new Thread(this,name);
        System.out.println("New Thread : "+ThreadB);
        ThreadB.start();
    }
   public void run()
   {
         for (int j=1; j<=5;j++)
              {
                  System.out.println(ThreadB.getName()+" : "+ j);
                try
                  {
                    ThreadB.sleep(500);
                }
                catch(InterruptedException e)
                {

                }

              }
    System.out.println("End of child Thread : "+ThreadB.getName());


   }
}


class C implements Runnable
 {

    String name;
    Thread ThreadC;
    C(String Threadname)
    {
        name=Threadname;
        ThreadC=new Thread(this,name);
        System.out.println("New Thread : "+ThreadC);
        ThreadC.start();
    }
   public void run()
   {
       for (int k=1; k<=5;k++)
       {
                  System.out.println(ThreadC.getName()+" : "+ k);
                  try
                  {
                    ThreadC.sleep(500);
                }
                catch(InterruptedException e)
                {

                }

          }
    System.out.println("End of child Thread : "+ThreadC.getName());


   }
}

class IsaliveJoinRunnable
{
public static void main(String args[])
{
    A a=new A("A");
    B b=new B("B");
    C c=new C("C");


    System.out.println("Thread A is alive : "+a.ThreadA.isAlive());

    System.out.println("Thread B is alive : "+b.ThreadB.isAlive());

    System.out.println("Thread C is alive : "+c.ThreadC.isAlive());

    for (int k=1; k<=5;k++)
    {
        System.out.println("Main Thread  : "+ k);
                try
                  {
                    Thread.sleep(500);
                }
                catch(InterruptedException e)
                {

                }
    }

    try
    {

    a.ThreadA.join();
    b.ThreadB.join();
    c.ThreadC.join();
    }
    catch(InterruptedException e)
    {
    System.out.println("Main Thread  interrupted");
    }

    System.out.println("End of main Thread!");

    System.out.println("Thread A is alive : "+a.ThreadA.isAlive());
    System.out.println("Thread B is alive : "+b.ThreadB.isAlive());
    System.out.println("Thread C is alive : "+c.ThreadC.isAlive());
}
}
READ MORE - Thread example in java

Extends Thread example

 class NamedThread extends Thread
 {
   private String name;  // The name of this thread.
   public NamedThread(String name)
   {  // Constructor gives name to thread.
      this.name = name;
   }
   public void run()
   {  // The run method prints a message to standard output.
         for (int i=1; i<=10;i++)
              {
                  System.out.println("Greetings from : "+name);
              }
    System.out.println("End of child thread!");


   }
}

class ExtendTest
{
public static void main(String args[])
{
    NamedThread greetings = new NamedThread("Child Thread");
    greetings.start();
    System.out.println("Thread has been started.");
    for (int i=1; i<=10;i++)
    {
        System.out.println("Main Thread ");
    }
    System.out.println("End of main thread!");
}
}
READ MORE - Extends Thread example

Draw and fill Oval using Applet and AWT

import java.awt.*;
import java.applet.*;
/*
<applet CODE="OvalExample.class" WIDTH=150 HEIGHT=150>
</applet>
*/
public class OvalExample extends Applet
{

    public void paint(Graphics g)
    {
        g.drawOval(20, 20, 50, 60);
        g.drawOval(90, 20, 50, 60);
        g.fillOval(30, 50, 30,30);
        g.fillOval(100,50,30,30);

    }
}
READ MORE - Draw and fill Oval using Applet and AWT

MouseEventsDemo using Applet and AWT

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
  <applet code="MouseEventsDemo" width=300 height=300>
  </applet>
*/

public class MouseEventsDemo extends Applet implements MouseListener, MouseMotionListener
{

    int x,y;

      public void init()
      {
           addMouseListener(this);
        addMouseMotionListener(this);

      }

      public void mouseClicked(MouseEvent me)
      {
        x=me.getX();
        y=me.getY();
        showStatus("Mouse clicked at "+x+","+y);
        repaint();
    }

    public void mouseEntered(MouseEvent me)
    {
        x=me.getX();
        y=me.getY();
        showStatus("Mouse entered at "+x+","+y);
        repaint();
    }

    public void mouseExited(MouseEvent me)
    {
        x=me.getX();
        y=me.getY();
        showStatus("Mouse exited at "+x+","+y);
        repaint();
    }

    public void mousePressed(MouseEvent me)
      {
        x=me.getX();
        y=me.getY();
        showStatus("Mouse pressed at "+x+","+y);
        repaint();
      }

      public void mouseReleased(MouseEvent me)
      {
        x=me.getX();
        y=me.getY();
        showStatus("Mouse released at "+x+","+y);
        repaint();
      }

    public void mouseDragged(MouseEvent me)
    {
        x=me.getX();
        y=me.getY();
        showStatus("Dragging mouse at "+x+","+y);

    }
     public void mouseMoved(MouseEvent me)
    {
        x=me.getX();
        y=me.getY();
        showStatus("Mouse moved at "+x+","+y);


    }

      public void paint(Graphics g)
      {

    }
}
READ MORE - MouseEventsDemo using Applet and AWT

Applet demo

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="Mouse" width=100 height=100>
<PARAM NAME=ButtonName VALUE=Save>
</applet>*/
public class ExerciseApplet extends Applet
{
public void paint(Graphics g)
{
    //Draw a Rectangle around the applet's display area.
       g.drawRect(0, 0, getWidth() - 1,getHeight() - 1);
    //Draw the current string inside the rectangle.
        g.drawString("Exercise Applet", 50, 50);
    }
}
READ MORE - Applet demo

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|