Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Am I doing it right?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Am I doing it right?

    This are my two classes:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
     
    public class Main {
     
        public static int number = 0;
     
        public static void main(String...args) {
            ExecutorService pool = Executors.newFixedThreadPool(3);
            while (number < 51) {
                for (int i = 0; i < 3; i++)
                    pool.execute(new Counter(i));
            }
            pool.shutdown();
        }
    }
     
    class Counter implements Runnable {
     
        private int number;
     
        public Counter(int number) {
            this.number = number;
        }
     
        @Override
        public void run() {
            synchronized (Main.class) {
                if (Main.number < 51) {
                    System.out.println("Thread "+ number +":"+ Main.number);
                    Main.number++;
                }
            }
        }
    }

    So this program will run and count from 0 to 50 by using three different threads. Is my code thread safe? Is there any better way how to write my code?
    Last edited by Slapy; April 10th, 2014 at 04:47 PM. Reason: merge to one code

  2. #2
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing it right?

    So here is it after few changes I made:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.atomic.AtomicInteger;
     
    public class Main {
     
        public static final AtomicInteger number = new AtomicInteger(0);
     
        public static void main(String...args) {
            ExecutorService pool = Executors.newFixedThreadPool(3);
            for (int i = 0; i < 3; i++)
                pool.execute(new Counter(i));
            pool.shutdown();
        }
    }
     
    class Counter implements Runnable {
     
        private int number;
     
        public Counter(int number) {
            this.number = number;
        }
     
        @Override
        public void run() {
            while (Main.number.intValue() < 49)
                synchronized (Main.number) {
                    System.out.println("Thread " + number + ":" + Main.number.getAndIncrement());
                }
        }
    }

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I doing it right?

    Does the code execute and do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing it right?

    Yes, I was just asking if there is any way how to do it better.
    Last edited by Slapy; April 10th, 2014 at 04:45 PM. Reason: words

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I doing it right?

    How do you expect the work to divided amongst the three threads? Does the code show show any spreading of the work?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing it right?

    It is just simpl program that have to count form 0 to 50 and to do so have to use 3 threads and output have to be:

    Thread 1: 0
    Thread 0: 1
    Thread 2: 2
    Thread 2: 3
    Thread 1: 4
    ...
    Thread 0: 50

    it does not matter on order of threads.
    Last edited by Slapy; April 11th, 2014 at 07:19 AM. Reason: error with threads

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I doing it right?

    What program are you executing for that output? The posted one has 3: 0->2,
    the output you show has 4: 0->3

    When I execute the code, 90% of the output is for thread 0. Sometimes thread 2 does nothing.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing it right?

    oh am so sorry. I messed it up. It should be just 3 threads not 4 and it is just luck which thread will be selected. For example this is one of my outputs.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I doing it right?

    My question was if you are satisfied with a multithread demo program that only executes 1 thread.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Am I doing it right?

    I think it should execute 3 threads:

    for (int i = 0; i < 3; i++)
                pool.execute(new Counter(i));

    or am I wrong?

    --- Update ---

    if you will try add after line with println Thread.sleep(10); surrounded with try/catch threads will have better chance to be switched.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Am I doing it right?

    As I said, the posted code executes one thread over 90% of the time. Often one of the threads is never executed.
    If you don't understand my answer, don't ignore it, ask a question.