Q:

interrupt main Thread from user thread using object class methods in java

    class A
        implements Runnable
    {
        int time;

        A( int time )
        {
            this.time = time;
        }

        public void run()
        {
            for ( int i = 0; i < 3; i++ )
            {
                System.out.println( Thread.currentThread().getName() );
                try
                {
                    Thread.sleep( time );
                }
                catch ( Exception e )
                {
                    System.out.println( e );
                }
                ;
            }
        }
    }
    class Check
    {
        public static void main( String args[] )
            throws Exception, Throwable
        {
            Thread t = Thread.currentThread();
            A a1 = new A( 200, t );
            A a2 = new A( 100, t );
            A a3 = new A( 500, t );
            Thread t1 = new Thread( a1 );
            Thread t2 = new Thread( a2 );
            Thread t3 = new Thread( a3 );
            t1.start();
            t2.start();
            t3.start();
            try
            {
                Thread.sleep( 5000 );
            }
            catch ( Exception e )
            {
            }
            ;// => i have to interrupt this sleep.
            System.out.println( "Main Thread's rest of the code" );
        }
    }

I want to interrupt the main Thread's sleep once when all the user threads are compeleted. If main Thread awakens before user thread completes then it must execute simultaneously along with other user threads.
p.s: This can be done with Object classes in java. So give me some suggestions using that.

A:

Using an ExecutorService would be much simpler, but assuming you need to use Threads directly....
All you need do is
t1.join();
t2.join();
t3.join();

to wait for all the thread to finish.
You can optimise this a little by using run()
t1.start();
t2.start();
t3.run(); // use the current thread

t1.join();
t2.join();
// only need to join two threads.

if main thread wakes up early then i have to perform main thread's rest of the code simultaneously along with the user threads.. 

Say you want to wait for the background thread but no more than 5 seconds (I can't imagine why you would do this but ...)
long end = System.currentTimeMillis() + 5000;
t1.join(Math.max(1, end - System.currentTimeMillis()));
t2.join(Math.max(1, end - System.currentTimeMillis()));
t3.join(Math.max(1, end - System.currentTimeMillis()));

The main thread will wait up to 5 seconds, finishing early if the threads are done, but will give up waiting if not.
To do similarly with an ExecutorService you can do
ExecutorService es = Executors.newCachedThreadPool();
es.submit(new A(200));
es.submit(new A(100));
es.submit(new A(500));

es.shutdown();
es.awaitTermination(5, TimeUnit.SECONDS);

