Thursday, October 12, 2023

Thread synchronization block

Using  synchronized Block

class Callme {

void call(String msg) {

System.out.print("[" + msg);

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

System.out.println("Interrupted");

}

System.out.println("]");

}

}

class Caller implements Runnable {

String msg;

Callme target;

Thread t;

public Caller(Callme targ, String s) {

target = targ;

msg = s;

t = new Thread(this);

t.start();

}

// synchronize calls to call()

public void run() {

synchronized(target) { // synchronized block

target.call(msg);

}

}

}

class Synch1 {

public static void main(String args[]) {

Callme target = new Callme();

Caller ob1 = new Caller(target, "Hello");

Caller ob2 = new Caller(target, "Synchronized");

Caller ob3 = new Caller(target, "World");

// wait for threads to end

try {

ob1.t.join();

ob2.t.join();

ob3.t.join();

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

}

}

Thread synchronization method

 Thread  synchronization

Before  synchronization

class Callme {

void call(String msg) {

System.out.print("[" + msg);

try {

Thread.sleep(1000);

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

System.out.println("]");

}

}

class Caller implements Runnable {

String msg;

Callme target;

Thread t;

public Caller(Callme targ, String s) {

target = targ;

msg = s;

t = new Thread(this);

t.start();

}

public void run() {

target.call(msg);

}

}

class Synch {

public static void main(String args[]) {

Callme target = new Callme();

Caller ob1 = new Caller(target, "Hello");

Caller ob2 = new Caller(target, "Synchronized");

Caller ob3 = new Caller(target, "World");

// wait for threads to end

try {

ob1.t.join();

ob2.t.join();

ob3.t.join();

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

}

}

----------------------------------------------------------------

After  synchronization :

class Callme {

synchronized void call(String msg) {

System.out.print("[" + msg);

try {

Thread.sleep(1000);

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

System.out.println("]");

}

}

class Caller implements Runnable {

String msg;

Callme target;

Thread t;

public Caller(Callme targ, String s) {

target = targ;

msg = s;

t = new Thread(this);

t.start();

}

public void run() {

target.call(msg);

}

}

class Synch {

public static void main(String args[]) {

Callme target = new Callme();

Caller ob1 = new Caller(target, "Hello");

Caller ob2 = new Caller(target, "Synchronized");

Caller ob3 = new Caller(target, "World");

// wait for threads to end

try {

ob1.t.join();

ob2.t.join();

ob3.t.join();

} catch(InterruptedException e) {

System.out.println("Interrupted");

}

}

}

Thread class methods isAlive(), join()

 Thread class methods isAlive(), join()

class NewThread implements Runnable{
    String name;
    Thread t;
    NewThread(String n){
        name = n;
        t = new Thread(this,name);
        System.out.println("Child: "+t);
        t.start();
    }
    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("Thread "+name+" Interrupted");
        }
        System.out.println(name+" Exiting");
    }
}
class AliveJoin {
    public static void main(String[] args){
        NewThread obj1 = new NewThread("One");
        NewThread obj2 = new NewThread("Two");
        NewThread obj3 = new NewThread("Three");
        System.out.println("Thread One is Alive: "+obj1.t.isAlive());
        System.out.println("Thread Two is Alive: "+obj2.t.isAlive());
        System.out.println("Thread Three is Alive: "+obj3.t.isAlive());
        try {
            System.out.println("Waiting for Threads");
            obj1.t.join();
            obj2.t.join();
            obj3.t.join();
        } catch (InterruptedException e) {
            System.out.println("Main Interrupted");
        }
        System.out.println("Thread One is Alive: "+obj1.t.isAlive());
        System.out.println("Thread Two is Alive: "+obj2.t.isAlive());
        System.out.println("Thread Three is Alive: "+obj3.t.isAlive());
    }
}


Thread Priorities with program

Thread Priorities with program 

public class Tpriority extends Thread {

   public void run() {
        System.out.println("Current Thread Name: " + Thread.currentThread().getName());
        System.out.println("Current Thread Priority: " + Thread.currentThread().getPriority());
    }
    public static void main(String[] args){
        Tpriority t1 = new Tpriority();
        Tpriority t2 = new Tpriority();
        t1.setName("One");
        t2.setName("Two");
        t1.setPriority(Thread.MAX_PRIORITY);
        t2.setPriority(Thread.MIN_PRIORITY);
        t1.start();
        t2.start();
    }
}

Machine Learning Course Assignments 2024(Dec)

 Machine Learning Course Assignments 2024 1)   Explain the concept of "generalization" in machine learning. Why is it a central go...