1.
What
is the significance of the functions Alive ( ) and join ( )?
The main thread must be the last thread to finish.
Sometimes this is accomplished by calling sleep() within main( ),
with a long enough delay to ensure that all child threads terminate prior to
the main thread. However, this is hardly a satisfactory solution, and it also
raises a larger question: How can one thread know when another thread has
ended? Fortunately, Thread provides a means by which you can answer this
question.
Two ways exist to determine whether a thread has
finished. First, you can call isAlive( ) on the thread. This method is
defined by Thread, and its general form is shown here:
final boolean isAlive( )
The isAlive( ) method returns true if
the thread upon which it is called is still running. It returns false otherwise.
While isAlive( ) is occasionally useful, the method that you will more
commonly use to wait for a thread to finish is called join( ), shown
here:
final void join( ) throws InterruptedException
This method waits until the thread on which it is
called terminates. Its name comes from the concept of the calling thread
waiting until the specified thread joins it. Additional forms of join(
) allow you to specify a maximum amount of time that you want to wait for
the specified thread to terminate. Here is an improved version of the preceding
example that uses join( ) to ensure that the main thread is the last to
stop. It also demonstrates the isAlive( ) method.
// Using join() to wait for threads to
finish.
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");}
System.out.println(name + " exiting.");
}
}
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");}
System.out.println(name + " exiting.");
}
}
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}
2.
Static variable and method
static is a keyword in java and we
can’t create a class or package name as static. Java static
keyword can be used in four cases.
a. Java
static variables: We can use static keyword with a class level variable. A static
variable is a class variable and doesn’t belong to Object/instance of the
class. Since static variables are shared across all the instances of Object,
they are not thread safe. Usually static variables are used with final keyword
for common resources or constants that can be used by all the objects. If the
static variable is not private, we can access it with ClassName.variableName
//static variable example
private static int count;
public static String str;
public static final String DB_USER = "myuser";
b. Java
static methods: Same as static variables, static methods belong to
class and not to class instances. A static method can access only static
variables of class and invoke only static methods of the class. Usually static
methods are utility methods that we want to expose to be used by other classes
without the need of creating an instance; for example Collections class. Java
Wrapper classes and utility classes contains a lot of static methods. The
main() method that is the entry point of a java program itself is a static
method.
//static method example
public static void setCount(int count) {
if(count > 0)
StaticExample.count = count;
}
//static util method
public static int addInts(int i, int...js){
int sum=i;
for(int x : js) sum+=x;
return sum;
}
c. Java
static Block: Java static block is the group of statements that gets
executed when the class is loaded into memory by Java ClassLoader. It is used
to initialize static variables of the class. Mostly it’s used to create static
resources when class is loaded. We can’t access non-static variables in static
block. We can have multiple static blocks in a class, although it doesn’t make
much sense. Static block code is executed only once when class is loaded into
memory.
static{
//can be used to initialize resources when class is loaded
System.out.println("StaticExample static block");
//can access only static variables and methods
str="Test";
setCount(2);
}
d. Java
Static Class: We can use static keyword with nested classes. static
keyword can’t be used with top-level classes. Static nested class is same as
any other top-level class and is nested for only packaging convenience.
3.
Interface
Polymorphism (from the Greek, meaning “many forms”) is a special
feature that allows one interface to be used for a general class of actions.
The specific action is determined by the exact nature of the situation. More
generally, the concept of polymorphism is often expressed by the phrase “one
interface, multiple methods”. This means that it is possible to design a
generic interface to a group of relative activities. This helps reduce
complexity by allowing the same interface to be used to specify a general class
of action. It is the compiler’s job to select the specific action (that is,
method) as it applies to each situation. User need only remember and utilize
the general interface.
interface A {
public void show();
}
interface B {
public void show();
}
interface C extends A, B {
//same method is declared in A and B both
public void show();
}
class Interfaces1
implements C {
public void show() {
System.out.println("Using Interface java implements
multiple inheritance");
}
public static void main(String[] args) {
A objA = new Interfaces1();
B objB = new Interfaces1();
C objC = new Interfaces1();
//all the method calls below are going to same concrete
implementation
objA.show();
objB.show();
objC.show();
}
}
Great information. Thanks for sharing.
ReplyDeleteBest MCA College in Lucknow
Great information. Thanks for sharing.
ReplyDeleteBest MCA College in Lucknow