Friday, May 21, 2021

Multi thread and Concurrency - Interruption 3 samples

 

public class Main1 {

    public static void main(String [] args) {

        Thread thread = new Thread(new BlockingTask());


        thread.start();

    }


    private static class BlockingTask implements Runnable {


        @Override

        public void run() {

            //do things

            try {

                Thread.sleep(500000);

            } catch (InterruptedException e) {

                System.out.println("Existing blocking thread");

            }

        }

    }

}

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


import java.math.BigInteger;


/**

 * Thread Termination & Daemon Threads

 * 

 */

public class Main2 {


    public static void main(String[] args) {

        Thread thread = new Thread(new LongComputationTask(new BigInteger("200000"), new BigInteger("100000000")));


        thread.start();

        thread.interrupt();

    }


    private static class LongComputationTask implements Runnable {

        private BigInteger base;

        private BigInteger power;


        public LongComputationTask(BigInteger base, BigInteger power) {

            this.base = base;

            this.power = power;

        }


        @Override

        public void run() {

            System.out.println(base + "^" + power + " = " + pow(base, power));

        }


        private BigInteger pow(BigInteger base, BigInteger power) {

            BigInteger result = BigInteger.ONE;


            for (BigInteger i = BigInteger.ZERO; i.compareTo(power) != 0; i = i.add(BigInteger.ONE)) {

// without this interruption, this won't be interrupt

                if (Thread.currentThread().isInterrupted()) {

                    System.out.println("Prematurely interrupted computation");

                    return BigInteger.ZERO;

                }

                result = result.multiply(base);

            }


            return result;

        }

    }

}


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


import java.math.BigInteger;


/**

 * Thread Termination & Daemon Threads

 * 

 */

public class Main3 {


    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(new LongComputationTask(new BigInteger("200000"), new BigInteger("100000000")));


// make thread deamon, run in background

        thread.setDaemon(true);

        thread.start();

        Thread.sleep(100);

        thread.interrupt();

    }


    private static class LongComputationTask implements Runnable {

        private BigInteger base;

        private BigInteger power;


        public LongComputationTask(BigInteger base, BigInteger power) {

            this.base = base;

            this.power = power;

        }


        @Override

        public void run() {

            System.out.println(base + "^" + power + " = " + pow(base, power));

        }


        private BigInteger pow(BigInteger base, BigInteger power) {

            BigInteger result = BigInteger.ONE;


            for (BigInteger i = BigInteger.ZERO; i.compareTo(power) != 0; i = i.add(BigInteger.ONE)) {

                result = result.multiply(base);

            }


            return result;

        }

    }

}


No comments:

Post a Comment