Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //1st
- package First;
- public class App {
- private static int counter = 0;
- public static void main(String[] args) {
- process();
- System.out.println(counter);
- }
- public static void process() {
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- for (int i = 0; i < 1000; i++) {
- counter++;
- }
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- for (int i = 0; i < 1000; i++) {
- counter++;
- }
- }
- });
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //2nd
- package Second;
- public class App {
- private static int counter=0;
- public static void main(String[] args) {
- process();
- System.out.println(counter);
- }
- public synchronized static void increment(){
- counter++;
- }
- public static void process(){
- Thread t1= new Thread(new Runnable(){
- public void run(){
- for (int i=0;i<1000000000;i++){
- increment();
- }
- }
- });
- Thread t2= new Thread(new Runnable(){
- public void run(){
- for (int i=0;i<1000000000;i++){
- increment();
- }
- }
- });
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //3rd
- package Third;
- public class App {
- public static int count1 = 0;
- public static int count2 = 0;
- public static void main(String[] args) {
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- compute();
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- compute();
- }
- });
- Thread t3 = new Thread(new Runnable() {
- public void run() {
- compute();
- }
- });
- t1.start();
- t2.start();
- t3.start();
- try {
- t1.join();
- t2.join();
- t3.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Count1=" + count1 + " - Count2=" + count2);
- }
- public static synchronized void add() {
- count1++;
- }
- public static void addAgain() {
- count2++;
- }
- public static void compute() {
- for (int i = 0; i < 100000; i++) {
- add();
- addAgain();
- }
- }
- }
- //4th
- package Fourth;
- public class App {
- private static int count1 = 0;
- private static int count2 = 0;
- private static Object lock1 = new Object();
- private static Object lock2 = new Object();
- public static void main(String[] args) {
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- incrementCount1();
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- incrementCount2();
- }
- });
- t1.start();
- t2.start();
- }
- public static void incrementCount1() {
- synchronized (lock1) {
- for (int i = 0; i < 10000000; i++) {
- count1++;
- System.out.println("Count1= " + count1);
- }
- }
- }
- public static void incrementCount2() {
- synchronized (lock2) {
- for (int i = 0; i < 10000000; i++) {
- count2++;
- System.out.println("Count2= " + count2);
- }
- }
- }
- }
- //5th
- package Fifth;
- class Processor {
- public void produce() throws InterruptedException {
- synchronized (this) {
- System.out.println("We are in producer method ...");
- wait();
- System.out.println("Again producer method....");
- }
- }
- public void consume() throws InterruptedException {
- Thread.sleep(3000);
- synchronized (this) {
- System.out.println("Consumer method.....");
- notify();
- }
- }
- }
- public class App {
- public static void main(String[] args) {
- Processor processor=new Processor();
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- try {
- processor.produce();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- try {
- processor.consume();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //6th
- package Sixth;
- import java.util.ArrayList;
- import java.util.List;
- class Processor {
- private List<Integer> list = new ArrayList<>();
- private final int LIMIT = 5;
- private final int BOTTOM = 0;
- private final Object lock = new Object();
- private int value = 0;
- public void producer() throws InterruptedException {
- synchronized (lock) {
- while (true) {
- if (list.size() == LIMIT) {
- System.out.println("Waiting for removing items from the list...");
- lock.wait();
- } else {
- System.out.println("Adding: " + value);
- list.add(value);
- value++;
- lock.notify();
- }
- Thread.sleep(500);
- }
- }
- }
- public void consumer() throws InterruptedException {
- synchronized (lock) {
- while (true) {
- if (list.size() == BOTTOM) {
- System.out.println("Waiting for adding items to the list...");
- lock.wait();
- } else {
- System.out.println("Removed: " + list.remove(--value));
- lock.notify();
- }
- Thread.sleep(500);
- }
- }
- }
- }
- public class App {
- public static void main(String[] args) {
- Processor processor = new Processor();
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- try {
- processor.producer();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- try {
- processor.consumer();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //7th
- package MethodSyn;
- public class App {
- public static int count1 = 0;
- public static int count2 = 0;
- public static void main(String[] args) {
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- incrementCount1();
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- incrementCount2();
- }
- });
- t1.start();
- t2.start();
- }
- public synchronized static void incrementCount1() {
- for(int i=0;i<1000;i++) {
- count1++;
- System.out.println("Count1= "+ count1);
- }
- }
- public synchronized static void incrementCount2() {
- for(int i=0;i<1000;i++) {
- count2++;
- System.out.println("Count2= "+ count2);
- }
- }
- }
- //8th
- package MethodSyn2;
- public class App {
- public static int count1 = 0;
- public static int count2 = 0;
- public static void main(String[] args) {
- Thread t1 = new Thread(new Runnable() {
- public void run() {
- incrementCount1();
- }
- });
- Thread t2 = new Thread(new Runnable() {
- public void run() {
- incrementCount2();
- }
- });
- t1.start();
- t2.start();
- }
- public static void incrementCount1() {
- synchronized (App.class) {
- for (int i = 0; i < 1000; i++) {
- count1++;
- System.out.println("Count1= " + count1);
- }
- }
- }
- public static void incrementCount2() {
- synchronized (App.class) {
- for (int i = 0; i < 1000; i++) {
- count2++;
- System.out.println("Count2= " + count2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement