public class RunnableExample {
public static void main(String[] args) {
Count cnt = new Count();
try {
while(cnt.mythread.isAlive()){
System.out.println("Main thread will be alive");
Thread.sleep(1500);
}
}
catch(InterruptedException e)
{
System.out.println("Main thread interuppted");
}
System.out.println("Main thread run is over");
}
}
class Count implements Runnable
{
Thread mythread;
Count(){
mythread = new Thread(this,"my runnable thread");
System.out.println("my thread created"+mythread);
mythread.start();
}
public void run(){
try{
for (int i=0; i<5; i++){
System.out.println("Printing the count"+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("my thread interuppted");
}
System.out.println("my thread run is over");
}
}
my thread createdThread[my runnable thread,5,main]
Printing the count 0
Main thread will be alive
Printing the count 1
Main thread will be alive
Printing the count 2
Main thread will be alive
Printing the count 3
Main thread will be alive
Printing the count 4
Main thread will be alive
my thread run is over
Main thread run is over
Here's an explanation of this output:
-
The "my runnable thread" is created, and its name and ID are printed.
-
The "my runnable thread" starts running, and it enters the
run
method. It prints "Printing the count 0" and then sleeps for 1 second. -
The main thread enters the while loop and prints "Main thread will be alive."
-
This pattern continues as the "my runnable thread" counts from 0 to 4, printing and sleeping.
-
After the "my runnable thread" completes its work, it prints "my thread run is over."
-
The main thread continues to execute the loop, periodically printing "Main thread will be alive" until it detects that the "my runnable thread" has finished.
-
Once the "my runnable thread" has finished, the main thread also prints "Main thread run is over," and the program terminates.
// Abstract class to represent a network plan
abstract class DataPlan {
String planType;
double callRate;
public DataPlan(String planType, double callRate) {
this.planType = planType;
this.callRate = callRate;
}
// Abstract method to calculate the cost
public abstract double calculateCost(int minutes);
// Method to display the bill
public void displayBill(int minutes) {
double cost = calculateCost(minutes);
System.out.println("The bill amount for " + planType + " of " + minutes + " units is: Rs. " + cost);
}
}
// Concrete network plan classes
class AMobile extends DataPlan {
public AMobile() {
super("AMobile", 1.50);
}
@Override
public double calculateCost(int minutes) {
return callRate * minutes;
}
}
class BMobile extends DataPlan {
public BMobile() {
super("BMobile", 1.75);
}
@Override
public double calculateCost(int minutes) {
return callRate * minutes;
}
}
class CMobile extends DataPlan {
public CMobile() {
super("CMobile", 1.50);
}
@Override
public double calculateCost(int minutes) {
return callRate * minutes;
}
}
public class SelectNetworkFactory {
public static DataPlan getNetworkPlan(String planType) {
if (planType.equals("AMobile")) {
return new AMobile();
} else if (planType.equals("BMobile")) {
return new BMobile();
} else if (planType.equals("CMobile")) {
return new CMobile();
} else {
return null; // Handle unknown plan types here
}
}
public static void main(String[] args) {
// Example usage:
String planType = "AMobile";
int minutes = 2;
DataPlan plan = getNetworkPlan(planType);
if (plan != null) {
plan.displayBill(minutes);
} else {
System.out.println("Unknown plan type.");
}
}
}