Sie sind auf Seite 1von 17

by: Deepti Mishra

In a class hierarchy, when a method in a subclass has the same name and

type signature as a method in its super class, then the method in the subclass is said to override the method of super class.
When an overridden method is called from within a subclass, it will always

refer to the version of that method defined by the subclass.


Method overriding occurs only when the names and the type signatures of

the two methods are identical.

//example of Method overriding.


class A { A() { System.out.println(inside super class constructor);

}
void show()//parent method { System.out.println(parent); } }

Class B extends A{

B(){
System.out.println(inside child class constructor); } void show() // overriden method

{
System.out.println(child); }} public class Override {

public static void main(String args[]) {


B subOb = new B(); subOb.show(); // this calls show() in B }}

Output:
Inside super class constructor Inside child class constructor child

If you wish to access the super class version of an overridden function, you can do so by using super. //previous program where we can access parent method class A { A() {

System.out.println(inside super class constructor);


} void show()//parent method {

System.out.println(parent);
}} Class B extends A{ B(){

System.out.println(inside child class constructor);


}

void show() // overridden method { super. show();//this will execute the parent class show() System.out.println(child); }} public class Override { public static void main(String args[]) { B subOb = new B(); subOb.show(); // this calls show() in B }} Output: Inside super class constructor Inside child class constructor parent child

what is multithreading: o Multithreading refers to two or more tasks executing concurrently within a single program. o Many threads can run concurrently within a program. Every thread in Java is created and controlled by the java.lang.Thread class. Multithreading has several advantages over Multiprocessing such as: I. Threads are lightweight compared to processes II. Threads share the same address space and therefore can share both data and code III. Context switching between threads is usually less expensive than between processes IV. Cost of thread intercommunication is relatively low that that of process intercommunication V. Threads allow different tasks to be performed concurrently

A thread can be in any of the following states:


New Thread state (Ready-to-run state) Runnable state (Running state)

Not Runnable state (blocked state)


Dead state

New Thread

the thread is in new state if you create an instance of Thread class but before the invocation of start() method.
Runnable the thread is in runnable state after invocation of start() method , but the thread scheduler has not selected it to be the running thread.

Not Runnable this is the state when the thread is still alive, but currently not eligible to run.

Dead State
A thread enters this state when the run() method has finished executing or when the stop() method is invoked. Once in this state, the thread cannot ever run again.

There are two ways to create thread in java: a. Implement the Runnable interface (java.lang.Runnable) b. By Extending the Thread class (java.lang.Thread) a. Implementing the Runnable Interface 1. A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object. 2. An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method. 3. The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned. 4. The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception.

// creation of Thread by implementing Runnable interface

Class mythread implements Runnable


{ Public void run() {

System.out.println(hello);
} } Class thread1{

Public static void main(String args[])


{ Mythread x=new mythread();//creating runnable object Thread t= new thread(x);//assigning this runneble object to Thread class object

t.start();
} }

Output:
hello b.Extending Thread Class

1. A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread.
2. This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call. 3. The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running. Class multi extends Thread{ Public void run(){ System.out.println(thread is running.);

}
Public static void main(String ar[]){

multi t1=new multi();

t1.start() ;
} } Output:

Thread is running.
Various Methods of Thread class: 1.public void run(): used to perform action for a thread. 2.public void start() :starts the execution of the thread.

3.public void sleep(long millisecond):causes the currently executing thread to sleep for specified number of milliseconds.
4.public void join():wait for thread to die.

5.Public void join(long millisecond): wait for thread to die for specified millisecond.
6.public int getPriority():returns the priority of the thread. 7.public int setPriority(int priority):change the priority.

8.public String getName():return the name of thread. 9.Public void setName(String name):change the name of the thread. 10.Public void suspend():is used to suspend the thread.

11.Public void resume(): is used to resume the suspended thread.


12.Public void stop(): used to stop the thread. 13.Public void interrupt():interrupt the thread. 14.Public bolean isInterrupted():testsif the thread has been interrupted. 15.Public boolean isAlive():tests if thread is alive.

The consumer producer problem (also known as the bounded-

buffer problem) is a classical example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue. The producer's job is to generate a piece of data, put it into the buffer and start again. At the same time, the consumer is consuming the data (i.e., Removing it from the buffer) one piece at a time. The problem is to make sure that the producer won't try to add data into the buffer if it's full and that the consumer won't try to remove data from an empty buffer. The solution for the producer is to either go to sleep or discard data if the buffer is full . The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again.

In the same way, the consumer can go to sleep if it finds the

buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer. The solution can be reached by means of inter-process communication, typically using semaphores.

Das könnte Ihnen auch gefallen