0% found this document useful (0 votes)
32 views11 pages

Ayush Java 4

This document contains examples of multithreading concepts in Java like threads, runnable interface, extending thread class. It also includes examples of get and set name methods, packaging, and inheritance hierarchy. The examples demonstrate threading concepts like creating and running multiple threads, implementing runnable interface by extending thread class, accessing variables from parent and child classes etc.

Uploaded by

Okby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

Ayush Java 4

This document contains examples of multithreading concepts in Java like threads, runnable interface, extending thread class. It also includes examples of get and set name methods, packaging, and inheritance hierarchy. The examples demonstrate threading concepts like creating and running multiple threads, implementing runnable interface by extending thread class, accessing variables from parent and child classes etc.

Uploaded by

Okby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Vidhyadeep insti. Of com. & info.

Technology, anita (kim)

Name :- RAMANI AYUSH

EN No :- E21110403000110108

Sub :- java (Assignment - 4)

Sem :- S.Y B.C.A Sem-4

PAGE NO:-1 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

1) THREAD AND RUNABLE INTERFACE


public class thread {

public static void main(String args[] )

int hh,mm,ss;

try

Thread t=new Thread("demo");

for(hh=0;hh<24;hh++)

for(mm=0;mm<=59;mm++)

for(ss=0;ss<=59;ss++)

System.out.println(hh+":"+mm+":"+ss);

t.sleep(1000);

}
PAGE NO:-2 SUB:-JAVA PREP BY:-RAMANI
AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

catch(Exception e)

System.out.println("Error:" + e);

OUTPUT:-

2 )IMPLEMENTING RUNNABLE INTERFACE


import java.lang.Thread;

public class implement {

implement()

Thread t=new Thread((Runnable) this,"demo thread");

System.out.println("Child thread" + t);

t.start();

public void run()

PAGE NO:-3 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

try {

for(int i=5;i>0;i--)

System.out.println("child tread:" + i);

Thread.sleep(500);

catch(InterruptedException e)

System.out.println("Child Interrupted");

System.out.println("Exit from child");

};

class drdemo

public static void main(String args[])

implement d=new implement();

try

for(int i=5;i>0;i--)

System.out.println("Main thread:" + i);

Thread.sleep(1000);

PAGE NO:-4 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

catch(InterruptedException e)

System.out.println("Main Interrupted");

System.out.println("Exit from main");

};

OUTPUT:-

3 )EXTENDING THREAD CLASS


import java.lang.Thread;

public class implement {

implement()

super();

System.out.println("Child thread" + this);

this.start();

private void start()

try {
PAGE NO:-5 SUB:-JAVA PREP BY:-RAMANI
AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

for(int i=5;i>0;i--)

System.out.println("child tread:" + i);

Thread.sleep(500);

catch(InterruptedException e)

System.out.println("Child Interrupted");

System.out.println("Exit from child");

};

class drdemo

public static void main(String args[])

implement d=new implement();

try

for(int i=5;i>0;i--)

System.out.println("Main thread:" + i);

Thread.sleep(1000);

PAGE NO:-6 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

catch(InterruptedException e)

System.out.println("Main Interrupted");

System.out.println("Exit from main");

};

OUTPUT:-

4) MULTI THREDING
public class Main {

public static void main(String args[]) {

new TestThread("TestThread1");

new TestThread("TestThread2");

new TestThread("TestThread3");

try {

Thread.sleep(15000);

} catch (InterruptedException e) {

System.out.println("Main thread is Interrupted");

System.out.println("Main thread is exiting.");

PAGE NO:-7 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

OUTPUT:-

5) GET NAME AND SET NAME


package mypack;

public class Test{

public static void main(String args[]){

Employee e=new Employee();//object is created

e.setName("priyanshi");//setting value to the object

System.out.println(e.getName());

private static class Employee {

public void setName(String priyanshi) {

public String getName() {

return null;

OUTPUT:-

PAGE NO:-8 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

6 )PACKAGE HELLO WORLD


package world;

public class HelloWorld {

public static void main(String args [])

System.out.println("Hello World");

OUTPUT:-

7) HEREACHOIY OF PACKAGE
class P {

int parentVariable = 5;

class C1 extends P {

int childVariable = 1;
PAGE NO:-9 SUB:-JAVA PREP BY:-RAMANI
AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

class C2 extends P {

int childVariable = 2;

class C3 extends P {

int childVariable = 3;

class main {

public static void main(String[] args) {

C1 child1 = new C1();

C2 child2 = new C2();

C3 child3 = new C3();

System.out.println("Parent variable + Child variable of child1 = " +


(child1.parentVariable + child1.childVariable));

System.out.println("Parent variable + Child variable of child2 = " +


(child2.parentVariable + child2.childVariable));

System.out.println("Parent variable + Child variable of child3 = " +


(child3.parentVariable + child3.childVariable));

OUTPUT:-

PAGE NO:-10 SUB:-JAVA PREP BY:-RAMANI


AYUSH
Vidhyadeep insti. Of com. & info. Technology, anita (kim)

PAGE NO:-11 SUB:-JAVA PREP BY:-RAMANI


AYUSH

You might also like