Skip to main content

Posts

Showing posts from July 11, 2012

isAlive() and join() in Thread

The main thread must be the last thread to finish. Sometimes this is accomplished by calling sleep() withinmain( ), with a long enough delay to ensure that all child threads terminate prior to the main thread. However, this is hardly a satisfactory solution, and it also raises a larger question: How can one thread know when another thread has ended? Fortunately, Thread provides a means by which you can answer this question.
Two ways exist to determine whether a thread has finished. First, you can call isAlive( ) on the thread. This method is defined by Thread, and its general form is shown here: final boolean isAlive( ) The isAlive( ) method returns true if the thread upon which it is called is still running. It returns falseotherwise. While isAlive( ) is occasionally useful, the method that you will more commonly use to wait for a thread to finish is called join( ), shown here: final void join( ) throws InterruptedException This method waits until the thread on which it is called terminat…

Ping or Poll a Machine via Java Thread

Ping a machine from java program




package com.test.demo;

import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;


public class PingTest implements Runnable {

        public static final int HOST_NOT_FOUND = 0;
        private static final int LOOKING_FOR_HOST = 1;
        public static final int CANNOT_CONNECT = 2;
        private static final int CONNECTING_TO_HOST = 3;
        public static final int SUCCESS = 4;

        private String hostname;
        private int port;
        private int status;

        private PingTest(String hostname, int port) {
                this.hostname = hostname;
                this.port = port;
        }

        public void run() {
                try {
Date dt1 = new Date();
System.out.println("Start==>"+dt1.toString());
status = LOOKING_FOR_HOST;
System.out.println("LOOKING_FOR_HOST......");
                        InetAddress addr = InetAddress.getByName(hostname);
                        status = CONNECTING_TO_…

Scheduling a java program

Scheduling a java program via timer /*** for one time scheduling*****/
package com.test.demo;

import java.util.Timer; import java.util.TimerTask;
/**  * Simple demo that uses java.util.Timer to schedule a task to execute once 2  * seconds have passed.  */
public class OnetimeScheduler {
  Timer timer;
  public OnetimeScheduler(int seconds) {     timer = new Timer();     timer.schedule(new RemindTask(), seconds * 1000);   }
  class RemindTask extends TimerTask {     public void run() {       System.out.println("Time's up!");       System.out.println("Hi");       timer.cancel(); //Not necessary because we call System.exit       //System.exit(0); //Stops everything     }   }
  public static void main(String args[]) {     System.out.println("About to schedule task.");     new OnetimeScheduler(2);     System.out.println("Task scheduled.");   } }


/**** for scheduling after every few seconds or mili seconds****/


package com.test.demo;
import java.u…