1)import java.io.
IOException;
public class cycle4_1{
public static void riskyMethod() throws IOException {
System.out.println("Inside riskyMethod");
throw new IOException("Simulated IOException");
}
public static void main(String[] args) {
try {
System.out.println("Inside try block");
riskyMethod();
} catch (IOException e) {
System.out.println("Caught exception: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
System.out.println("Program continues after try-catch-finally");
}
}
2)
public class cycle4_2
{
public static void main(String[] args)
{
try{
dividenumbers(10,0);
}
catch(ArithmeticException e)
{
System.out.println("caught ArithmeticException.\n"+
e.getMessage());
}
try
{
String str="1w9u76";
int integer=Integer.parseInt(str);
System.out.println(integer);
}
catch(NumberFormatException e)
{
System.out.println("caught NumberFormatException.\n"+
e.getMessage());
}
try
{
int[] arr={1,2,8,9};
System.out.println(arr[-1]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("caught ArrayIndexOutOfBoundsException.\n"+
e.getMessage());
}
try
{
String str=null;
int l=str.length();
System.out.println();
}
catch(NullPointerException e)
{
System.out.println("caught NullPointerException.\n"+
e.getMessage());
}
catch(Exception e)
{
System.out.println("caught exception.\n"+ e.getMessage());
}
finally
{
System.out.println("finally block executed.");
}
public static void dividenumbers(int num1,int num2)
{
if(num2==0)
{
throw new ArithmeticException("cannot divide by zero.\n");
}
int result=num1/num2;
System.out.println("Result: "+ result);
}
}
4)// Shared resource class
class Counter {
private int count = 0;
// Synchronized method to increment the counter
public synchronized void increment() {
count++;
}
// Method to return the counter value
public int getCount() {
return count;
}
}
// Thread class that increments the counter
class CounterThread extends Thread {
private Counter counter;
// Constructor to initialize the shared counter object
public CounterThread(Counter counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
counter.increment(); // Increment the counter
}
}
}
public class ThreadSynchronizationDemo {
public static void main(String[] args) {
// Creating a shared Counter object
Counter counter = new Counter();
// Creating two threads that share the same Counter object
CounterThread t1 = new CounterThread(counter);
CounterThread t2 = new CounterThread(counter);
// Starting both threads
t1.start();
t2.start();
// Waiting for both threads to finish execution
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// Printing the final count
System.out.println("Final Count: " + counter.getCount());
}
}
5)// Thread to print odd numbers between 1 and 100
class OddThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
System.out.println("Odd: " + i);
}
}
}
}
// Thread to print even numbers between 1 and 100
class EvenThread extends Thread {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println("Even: " + i);
}
}
}
}
public class OddEvenThreads {
public static void main(String[] args) {
// Creating thread for odd numbers
OddThread oddThread = new OddThread();
// Creating thread for even numbers
EvenThread evenThread = new EvenThread();
// Starting both threads
oddThread.start();
evenThread.start();
// Ensuring both threads complete before finishing the program
try {
oddThread.join();
evenThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
6)
// Thread class that displays numbers
class PriorityThread extends Thread {
public PriorityThread(String name) {
super(name); // Assign a name to the thread
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(getName() + ": " + i);
try {
Thread.sleep(500); // Sleep for half a second
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
}
public class ThreadPriorityDemo {
public static void main(String[] args) {
// Create three threads
PriorityThread t1 = new PriorityThread("Thread 1 (Low Priority)");
PriorityThread t2 = new PriorityThread("Thread 2 (Normal Priority)");
PriorityThread t3 = new PriorityThread("Thread 3 (High Priority)");
// Set priorities for the threads
t1.setPriority(Thread.MIN_PRIORITY); // Priority 1
t2.setPriority(Thread.NORM_PRIORITY); // Priority 5
t3.setPriority(Thread.MAX_PRIORITY); // Priority 10
// Start the threads
t1.start();
t2.start();
t3.start();
// Ensure main thread waits for others to finish
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}