Learning Java, why don’t I get some thread overlap?

I tried the following code:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;


    public class Main {
        static int i = 0;

    public static void main(String[] args) {

        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
        new Thread(t4).start();
        new Thread(t5).start();
        new Thread(t6).start();
    }

    private static void countMe(String name){
        i++;
        System.out.println("Current Counter is: " + i + ",updated by: " + name);
    }

    private static Runnable t1 = new Runnable() {
        public void run() {
            try{
                for(int i=0; i<2; i++){
                    countMe("t1");
                }
            } catch (Exception e){}

        }
    };

    private static Runnable t2 = new Runnable() {
        public void run() {
            try{
                for(int i=0; i<2; i++){
                    countMe("t2");
                }
            } catch (Exception e){}
       }
    };
        private static Runnable t3 = new Runnable() {
        public void run() {
            try{
                for(int i=0; i<2; i++){
                    countMe("t3");
                }
            } catch (Exception e){}
       }
    };
        private static Runnable t4 = new Runnable() {
        public void run() {
            try{
                for(int i=0; i<2; i++){
                    countMe("t4");
                }
            } catch (Exception e){}
       }
    };
        private static Runnable t5 = new Runnable() {
        public void run() {
            try{
                for(int i=0; i<2; i++){
                    countMe("t5");
                }
            } catch (Exception e){}
       }
    };
        private static Runnable t6 = new Runnable() {
        public void run() {
            try{
                for(int i=0; i<2; i++){
                    countMe("t6");
                }
            } catch (Exception e){}
       }
    };
}

And the output is obtained on ideone:

Current counter: 1, updated by: T1, current counter: 2, updated by: T1, current counter: 3, updated by: T2, current counter: 4, updated by: T2, current counter: 5, updated by: T3, current counter: 6, updated by: T3, current counter: 7, updated by: T4, current counter: 8, updated by: T4, current counter: 9, Updated by: t5current counter: 10, updated by: t5current counter: 11, updated by: t6current counter: 12, updated by: T6

It seems that everything is done in a linear way, that is, one thread by one called the function countme in the order I created them Multithreading means that they may fail I'm missing here what? Is the configuration of the machine I run (I tried on ideone. Com) such that it runs threads and is created sequentially?

Solution

Thread creation is expensive What may happen is that when you finish starting thread 2, thread 1 has completed When thread 3 starts doing its job, thread 2 has finished wait.

Insert a hexagonal cyclic barrier at the beginning of the thread function and see their race (you may even lose some I increments because I can't guarantee that they are atomic)

If this is not enough to reliably trigger a race, let the thread do more work

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>