学习Java,为什么我没有得到一些线程的重叠?



我尝试了以下代码:

/* 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){}
       }
    };
} 

在ideone上得到了输出: Current Counter is: 1, updated by: t1 Current Counter is: 2, updated by: t1 Current Counter is: 3, updated by: t2 Current Counter is: 4, updated by: t2 Current Counter is: 5, updated by: t3 Current Counter is: 6, updated by: t3 Current Counter is: 7, updated by: t4 Current Counter is: 8, updated by: t4 Current Counter is: 9, updated by: t5 Current Counter is: 10, updated by: t5 Current Counter is: 11, updated by: t6 Current Counter is: 12, updated by: t6 似乎一切都在以线性的方式进行,即被称为函数countMe的线程按照我创建它们的顺序一个接一个地进行。多线程并不意味着它们可能会出现故障。我在这里错过了什么?我正在运行的机器(我在ideone.com上尝试过)的配置方式是按照创建线程的顺序运行线程吗?

创建线程的成本很高。可能发生的情况是,当您完成启动线程2时,线程1已经完成。当线程3开始执行它的任务时,线程2已经完成。等等。

在线程函数的开头插入一个六方循环屏障,并看到它们竞争(甚至可能失去一些i增量,因为i++不能保证是原子的)。

如果这还不足以可靠地触发竞争,那么让线程做更多的工作。

线程启动需要时间。它们可能需要毫秒或数百微秒。如果这听起来不长,那么考虑一下你的CPU在这段时间内可以执行一百万条指令。即循环在下一个线程有机会开始之前完成。

一个简单的方法是:A)有一个像10+这样的较长循环,b)添加一个像Thread.sleep(1000)这样的延迟;您可以看到线程同时运行。

我还建议您使用AtomicInteger而不是int,因为i++不是线程安全的。

最新更新