小编Raj*_*wal的帖子

Java多线程:意外的结果

我正在开发一个企业应用程序.我在多线程环境中运行应用程序时遇到了一些问题.我正在编写一个程序,其中有一个变量,其值以非常快的速率更新(递增)(例如10000次更新/ persecond).循环运行一定的迭代,变量的值递增并存储在HashMap中.一旦循环终止并且值打印HashMap中的变量.我得到了变量的意外价值.

这是演示程序(请阅读评论以便更好地理解):

class test implements Runnable {

    static ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
    static AtomicInteger value_to_be_incremented_stored = new AtomicInteger(0); // variable whose value to be updated
    static AtomicInteger i = new AtomicInteger(0);  // this runs the loop

    @Override
    public void run() {

        for (i.set(0); i.get() < 100000; i.incrementAndGet()) {
            /*
                This loop should run 100000 times and when loop terminates according to me value of variable 
                "value_to_be_incremented_stored" should be 100000 as its value is incremented 
                100000 times the …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

查找 WebEngine 中打开的当前网页的标题 [[JAVAFX]]

我正在编写一个基于 Javafx 的 Web 浏览器。我想获取当前在 WebEngine 中打开的网页的标题。谢谢 :)

javafx javafx-8 javafx-webengine

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

为什么这个基本的java程序没有编译

我在编译这个程序时遇到错误:

class ArraysInMethods {
    public static void main(String[] args) {
        int array[]={1,6,2,5,3,8,9,0,5};
        Add5(array);
        for(int y : array){
            System.out.println(y);
        }
    }
    public void Add5(int x[]){
        for(int counter=0; counter < x.length; counter++){
            x[counter]+=5;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Cannot make a static reference to the non-static method Add5(int[]) from the type ArraysInMethods
        at ArraysInMethods.main(ArraysInMethods.java:6)
Run Code Online (Sandbox Code Playgroud)

java arrays methods

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