STIW3054-A172 / Main-Issues

1 stars 1 forks source link

Exercise_07 #8

Closed zhamri closed 6 years ago

zhamri commented 6 years ago

Write a Java program to count integer numbers from 1 to 20. If the number reach to 15, then the program should count down and stop at 5. You must use java.util.concurrent.atomic.AtomicInteger to solve this problem.

umarabdullah commented 6 years ago
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stiw3054_assignment.excercise;

/**
 *
 * @author abdullah
 */
import java.util.concurrent.atomic.AtomicInteger;

class TestAtomicInteger1s {

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

        CountSolution pt = new CountSolution();
        Thread t1 = new Thread(pt, "t1");
        t1.start();

    }

}

class CountSolution implements Runnable {

    private AtomicInteger count = new AtomicInteger();  //New

    @Override
    public void run() {
        System.out.println("Counting ...");
        for (int i = 1; i <= 30; i++) {
            processSomething(i);
            if ((count.get() <= 16)&&(i<16))
            System.out.print(count.incrementAndGet() + " ");
            else {
               if(count.get()==15)
               System.out.println("\nDecreamenting ....");
               if(count.get()!=5)
                System.out.print(count.decrementAndGet() + " ");
               if(count.get()<=5)
                 break; }

        }
    }

    public int getCount() {
//        return this.count;
        return this.count.get();  //New
    }

    private void processSomething(int i) {
        try {
            Thread.sleep(1000);
            //if(i != 15)
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
screen shot 2018-03-28 at 11 50 32 am
diyanazaidi commented 6 years ago

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.issue;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author User
 */
public class issue07 {
    public static void main (String [] args){
        countdown c = new countdown();
        Thread t1 = new Thread(c, "t1");
        t1.start();
    }

}

class countdown implements Runnable{

     private AtomicInteger count = new AtomicInteger();

    @Override
    public void run() {
        count.incrementAndGet(); 
        for (int i = 1; i <= 20; i++) {
            if(i==15){
                countdownFound(i);
            }

            count.incrementAndGet(); 
        }
    }
    private void countdownFound(int i){
        int z = i;
        try {

            for(int x = z;x>=5;x--){
                System.out.println(count.get());
                Thread.sleep(1500);
                count.decrementAndGet();
            }
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

image

thiviya commented 6 years ago
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package issue7;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author thiviya
 */
public class Issue7 {

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

        CountSolution pt = new CountSolution();
        Thread t1 = new Thread(pt, "t1");
        t1.start();

    }

}

class CountSolution implements Runnable {

    private AtomicInteger count = new AtomicInteger();  //New

    @Override
    public void run() {
        System.out.println("Counting ...");
        for (int i = 1; i <= 30; i++) {
            if ((count.get() <= 16)&&(i<16))
            System.out.print(count.incrementAndGet() + " ");
            else {
               if(count.get()==15){
               System.out.println();
               }
               if(count.get()!=5)
                System.out.print(count.decrementAndGet() + " ");
               if(count.get()<=5)
                 break; }

        }
    }

}

image

Reference https://stackoverflow.com/questions/7572434/how-to-implement-a-concurrent-circular-ticker-counter-in-java https://www.journaldev.com/1095/atomicinteger-java

AsadRazali commented 6 years ago

import java.util.concurrent.atomic.AtomicInteger;

    class countinteger implements Runnable
    {

     private AtomicInteger counter= new AtomicInteger();

    public void run() 
    {
      System.out.print(counter.incrementAndGet() + " ");
        for (int i = 1; i <=20; i++) 
        {

            if(i<15)
            {
            System.out.print(counter.incrementAndGet() + " ");
            }
               if(i==15)
            {
                for(int k =i; k>5 ; k--)
                {
            System.out.print(counter.decrementAndGet() + " ");
                }
            }

        }
    }

    } 

public class E7 {
    public static void main (String [] args)
    {
        countinteger ci = new countinteger();
        Thread t1 = new Thread(ci, "t1");
        t1.start();
    }

}

2

Refference

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b27/java/util/concurrent/atomic/AtomicInteger.java#AtomicInteger.decrementAndGet%28%29

https://examples.javacodegeeks.com/core-java/util/concurrent/atomic/atomicinteger/java-atomicinteger-example/

kzkit commented 6 years ago

Source Code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.stiw3054_maven;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author aspire
 */
public class atomic {
  public static void main(String[] args) {
        AtomicInteger counter = new AtomicInteger(0);

        for(int i =0;i<20;i++){
            counter.incrementAndGet();
            System.out.print(counter.get());

           if ( counter.get() == 15){
               i=19;

       }
        }
        System.out.println("");
        for(int i =0;i<15;i++){
            counter.decrementAndGet();
            System.out.print(counter.get());

           if ( counter.get() == 5){
               i=14;

       }
        }
        System.out.println("");
       System.out.println("Current value ="+ counter.get());
  }
}

Screenshot

issue8

References

http://www.javacodex.com/Concurrency/AtomicInteger-Counter

adamrustam commented 6 years ago
import java.util.concurrent.atomic.AtomicInteger;

public class Issue8 {
  public static void main(String[] args) {
        AtomicInteger counter = new AtomicInteger();
        //int limit = 15;
        for (int x = 0; x < 20; x++) {
            counter.incrementAndGet();
            //counter.incrementAndGet();
            //System.out.println(counter.get());

            if (counter.get()==15) {

                    while(counter.get()!=4) {
                        System.out.println(counter.getAndDecrement());
                    }

            }

        }

  }

}

Output

screen shot 2018-03-28 at 12 17 18 pm

References

http://www.codejava.net/java-core/concurrency/understanding-atomic-variables-in-java https://stackoverflow.com/questions/4818699/practical-uses-for-atomicinteger?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

saufisyafiq commented 6 years ago

Main Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stiw3054.atomicissue;

/**
 *
 * @author Saufi
 */

public class Main {
    public static void main (String [] args){
        runDecrement r = new runDecrement();

    }

}

runDecrement Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stiw3054.atomicissue;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author Saufi
 */
class runDecrement implements Runnable{

     private final AtomicInteger atomic = new AtomicInteger();

    @Override
    public void run() {
        atomic.incrementAndGet(); 
        for (int i = 1; i <= 20; i++) {
            if(i==15){
                startdesc(i);
            }

            atomic.incrementAndGet(); 
        }
    }

    private void startdesc(int x){
        int desc = x;
        for( x = desc;x>=5;x--){
          int  currentValue;
            currentValue = atomic.get();
            System.out.println(currentValue);
            atomic.decrementAndGet();
        }
    }

}

OUTPUT atomicissue

Nurzyra commented 6 years ago

package exercise7;

import java.util.concurrent.atomic.AtomicInteger;

public class Exercise7 {

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

        CountSolution pt = new CountSolution();
        Thread t1 = new Thread(pt, "t1");
        t1.start();

    }

}

class CountSolution implements Runnable {

    private AtomicInteger count = new AtomicInteger();  

    @Override
    public void run() {
        System.out.println("Counting ...");
        for (int i = 1; i <= 30; i++) {
            if ((count.get() <= 16)&&(i<16))
            System.out.print(count.incrementAndGet() + " ");

            else {
               if(count.get()==15){
               System.out.println();
               }
               if(count.get()!=5)
                System.out.print(count.decrementAndGet() + " ");
               if(count.get()<=5)
                 break; }

        }
    }

}

output : 1

alifhaikal commented 6 years ago

Source Code


package com.mycompany.exercise7;

import java.util.concurrent.atomic.AtomicInteger;

class TestAtomicInteger1s {

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

        CountSolution pt = new CountSolution();
        Thread t1 = new Thread(pt, "t1");
        t1.start();

    }

}

class CountSolution implements Runnable {

    private AtomicInteger count = new AtomicInteger();  //New

    @Override
    public void run() {
        System.out.println("Count");
        for (int i = 1; i <= 30; i++) {
            processSomething(i);
            if ((count.get() <= 16)&&(i<16))
            System.out.print(count.incrementAndGet() + " " + "\n");
            else {
               if(count.get()==15)
               System.out.println("\nDecrement");
               if(count.get()!=5)
                System.out.print(count.decrementAndGet() + " " + "\n");
               if(count.get()<=5)
                 break; }

        }
    }

    public int getCount() {
//        return this.count;
        return this.count.get();  //New
    }

    private void processSomething(int i) {
        try {
            Thread.sleep(1000);
            //if(i != 15)
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

1 2

thilagan7 commented 6 years ago

package atomic;

/*

class countinteger implements Runnable {

private AtomicInteger count = new AtomicInteger();  

@Override
public void run() {
    System.out.println("Counting .....");
    for (int i = 1; i <= 30; i++) {
        if ((count.get() <= 16)&&(i<16))
        System.out.print(count.incrementAndGet() + " ");

        else {
           if(count.get()==15){
           System.out.println();
           }
           if(count.get()!=5)
            System.out.print(count.decrementAndGet() + " ");
           if(count.get()<=5)
             break; }

    }
}

} public class Atomic {

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

    countinteger pt = new countinteger();
    Thread t1 = new Thread(pt, "t1");
    t1.start();

}

} untitled1

rubenmuhajir commented 6 years ago

package com.mycompany.stiw3054rt;

import java.util.concurrent.atomic.AtomicInteger;

/*

public class exercise7 { public static void main(String[] args) { AtomicInteger counter = new AtomicInteger();

    //int limit = 15;
    for (int x = 0; x < 15; x++) {
        counter.incrementAndGet();
        System.out.print(counter.get() + " ");

         if (counter.get()==15) {

            while(counter.get()!=4) {
                counter.decrementAndGet();
                System.out.print(counter.get() + " ");

            }
        }

    }
  }

}

image

syaba314 commented 6 years ago
import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author ASUS
 */
public class atomicInteger {
public static void main(String[] args) {

    AtomicInteger counter = new AtomicInteger();

     for(int i =0;i<=20;i++){

            System.out.print(counter.incrementAndGet()+" ");

           if ( counter.get() == 15){

            while((counter.get() !=5) ) {
                System.out.print(counter.decrementAndGet()+" ");

                }
               }
           }
     }
} 
capture
AdlanShahjehan commented 6 years ago
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
  public static void main(String[] args) {
        AtomicInteger counter = new AtomicInteger(0);

         System.out.println("Counting..");

        for(int i =0;i<20;i++){
            counter.incrementAndGet();
            System.out.print(counter.get()+" ");

           if ( counter.get() == 15){
               i=19;  
            }
        }
        System.out.println();
        System.out.println("Counting Down..");

        for(int i =0;i<15;i++){
            counter.decrementAndGet();
            System.out.print(counter.get()+" ");

           if ( counter.get() == 5){
               i=14;      
            }
        }
       System.out.println("");
  }
}

Output 7