Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class thread implements Runnable
- {
- Thread t;
- thread()//constructor for initializing the class
- {
- t=new Thread(this,"Demo");//intialializing this thread using this..
- System.out.println("Child Thread ");
- t.start();//starting the thread by calling start() function.it calls the run function
- }
- public void run()
- {
- try
- {
- for(int i=0;i<10;i++)
- {
- System.out.println("Fuck All");
- Thread.sleep(500);//interruption of 500 ms
- }
- }catch(InterruptedException e)//sleep method can throw exception.to handle this exception
- {
- System.out.println("Child Thread interrupted");
- }
- System.out.println("Exiting Child Thread");
- }
- }
- public class NewThread {
- public static void main(String args[])
- {
- new thread();
- try
- {
- for(int i=0;i<10;i++)
- {
- System.out.println("Main Thread "+i);
- Thread.sleep(1000);//same as before
- }
- }catch(InterruptedException e)
- {
- System.out.println("Main Thread Interrupted");
- }
- System.out.println("Main thread Exiting");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement