You are on page 1of 4

Program-1 // Create two classes both having two threads. One displays Hello and another displays World.

Create one class by implementing the !nnable "nter#ace and another by e$tending #rom %hread class.

class &ewt e$tends %hread ' &ewt() ' s!per(*%hread*)+ start()+ , p!blic void r!n() ' try ' -ystem.o!t.println(*%hreads e$tending %hread class . *)+ -ystem.o!t.println(*Hello*)+ %hread.sleep(1/)+ -ystem.o!t.println(*World*)+ %hread.sleep(1/)+ , catch("nterr!pted0$ception e) ' -ystem.o!t.println(*0$iting thread*)+ , , ,

class &ewthread implements !nnable ' %hread t+ &ewthread()' t1new %hread(this2*%hread*)+ t.start()+ , p!blic void r!n() ' try' %hread.sleep(3/)+ -ystem.o!t.println(*%hreads implementing r!nnable inter#ace . *)+ -ystem.o!t.println(*Hello*)+ %hread.sleep(3/)+ -ystem.o!t.println(*World*)+ , catch("nterr!pted0$ception e) ' -ystem.o!t.println(*%hread interr!pted*)+ , , , class 0$tendthread ' p!blic static void main(-tring args45) ' new &ewt()+ new &ewthread()+ ,

Program-6 // Create a class that will accept !ser name and validate whether the #ield is empty or not. 7t the same time a cloc8 will be displayed to show the c!rrent time. import 9ava.!til.:ate+ import 9ava.io.;+ class cloc8' p!blic static void main(-tring args45) ' :ate d1new :ate()+ -ystem.o!t.println(d)+ "np!t-tream eader isr1new "np!t-tream eader(-ystem.in)+ <!##ered eader br1new <!##ered eader(isr)+ -ystem.o!t.println(*Please enter !sername . *)+ try' -tring str1new -tring()+ str1br.read=ine()+ i#(str.length()11/) -ystem.o!t.println(*%he !sername #ield is empty*)+ else -ystem.o!t.println(*%he !sername #ield is not empty*)+ , catch("O0$ception e) ' -ystem.o!t.println(*0$ception ca!ght.*)+ , ,,

You might also like