小编Rod*_*ver的帖子

为什么我的for循环执行时间不变?

public class Test {

    public static void main(String[] args) {

        int x = 150_000;

        long start = System.currentTimeMillis();
        for(int i = 0; i < x; i++) {            
            f1(i);
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) / 1000.0);
    }

    private static long f1(int n) {
        long x = 1;
        for(int i = 0; i < n; i++) {
            x = x + x;
        }
        return x;
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么x设置为150_000或者4_000_000甚至2_000_000_000不改变这个循环的执行时间?

java performance jvm performance-testing

6
推荐指数
1
解决办法
144
查看次数

C#BackgroundWorker和Invoke

有人可以解释为什么Thread.SleepBackgroundWorker块执行调用它.调用应该导致委托在UI线程上执行,后台线程应该继续执行.但这不会发生 - 为什么?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        BackgroundWorker bgrw = new BackgroundWorker();
        bgrw.DoWork += new DoWorkEventHandler(bgrw_DoWork);

        bgrw.RunWorkerAsync();
    }

    void bgrw_DoWork(object sender, DoWorkEventArgs e)
    {
        Console.WriteLine(DateTime.Now);
        this.Invoke(new Action(() => { Thread.Sleep(2000); })); //should be executed on the UI thread
        Console.WriteLine(DateTime.Now); // This line is executed after 2 seconds
    }       
}
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading invoke backgroundworker

1
推荐指数
1
解决办法
1856
查看次数