Is there a practical use for weak references?

Possible Duplicate: Weak references - how useful are they? Since weak references can be claimed by the garbage collector at any time, is there any practical reason for using them? If you want to keep a reference to something as long as it is used elsewhere eg a Listener, you can use a weak reference. WeakHashMap can be used as a short lived cache of keys to derived data. It can also be u

对于弱引用有实际用途吗?

可能重复: 弱引用 - 它们有多有用? 由于垃圾收集器可以随时声明弱引用,因此使用它们有什么实际的理由吗? 如果只要在其他地方(例如Listener)使用某个引用,就可以使用弱引用。 WeakHashMap可以用作派生数据的密钥的短暂缓存。 它也可以用来保存有关使用的对象的信息,而不知道何时抛弃这些对象。 BTW软引用就像弱引用,但它们不会总是立即清除。 GC会一直放弃弱参考,并在可能时保留软参考。 还有一种称为幻

Cost of using weak references in Java

Has anyone researched the runtime costs involved in creating and garbage collecting Java WeakReference objects? Are there any performance issues (eg contention) for multi-threaded applications? EDIT: Obviously the actual answer(s) will be JVM dependent, but general observations are also welcome. EDIT 2: If anyone has done some benchmarking of the performance, or can point to some benchmarkin

在Java中使用弱引用的代价

有没有人研究过创建和垃圾收集Java WeakReference对象所涉及的运行时成本? 多线程应用程序是否存在性能问题(如争用)? 编辑:显然实际的答案将是JVM的依赖,但一般的观察也是受欢迎的。 编辑2:如果有人已经做了一些性能基准测试,或者可以指出一些基准测试结果,那将是理想的。 (对不起,但赏金已过期...) WeakReference对CMS垃圾回收器有负面影响。 就我从服务器的行为中可以看出,它影响并行备注阶段时间。 在

Converting array to list in Java

How do I convert an array to a list in Java? I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour. For example: int[] spam = new int[] { 1, 2, 3 }; Arrays.asList(spam) on 1.4.2 returns a list containing the elements 1, 2, 3 on 1.5.0+ returns a list containing t

在Java中将数组转换为列表

如何将数组转换为Java中的列表? 我使用了Arrays.asList()但行为(和签名)以某种方式从Java SE 1.4.2(现在存档的文档)更改为8,并且我在网上找到的大多数代码片段都使用1.4.2行为。 例如: int[] spam = new int[] { 1, 2, 3 }; Arrays.asList(spam) 在1.4.2上返回一个包含元素1,2,3的列表 在1.5.0+上返回一个包含数组垃圾邮件的列表 在很多情况下,应该很容易发现,但有时可能会被忽视: Assert.assertTrue(Arrays

counting number of ones in binary representation of a number

Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? I want to find out how many 1s are there in binary representation of a number.I have 2 logic . int count =0; int no = 4; while(no!=0){ int d = no%2; if(d==1) count++; no = no/2; str = str+ d; } Now second logic is to keep on masking number iteratively with 1,2,4,8,32 and check

计算一个数字的二进制表示的个数

可能重复: 计算32位整数中设定位数的最佳算法? 我想知道在一个数字的二进制表示中有多少个1。我有2个逻辑。 int count =0; int no = 4; while(no!=0){ int d = no%2; if(d==1) count++; no = no/2; str = str+ d; } 现在第二个逻辑是继续用1,2,4,8,32迭代屏蔽数字,并检查结果是否为1,2,4,8 .....我不知道该循环的结束条件是什么。 使用Java API(Java 5或更高版本)。 Integer.bitCount(i

How to count the number of 1's a number will have in binary?

Possible Duplicate: Best algorithm to count the number of set bits in a 32-bit integer? How do I count the number of 1 'sa number will have in binary? So let's say I have the number 45 , which is equal to 101101 in binary and has 4 1 's in it. What's the most efficient way to write an algorithm to do this? Instead of writing an algorithm to do this its best to use the bui

如何计算一个数字在二进制中的数量?

可能重复: 计算32位整数中设定位数的最佳算法? 我怎么算的数1 “SA数量将在二进制? 所以我们假设我的数字是45 ,等于二进制101101 ,并且有4 1 。 什么是编写算法的最有效的方法来做到这一点? 而不是写一个算法来做到这一点,最好使用内置函数。 Integer.bitCount() 是什么让这个特别有效的是JVM可以把它当作一个内在的东西。 即在支持它的平台(例如Intel / AMD)上用单个机器代码指令识别并替换整个事物

Please explain the logic behind Kernighan's bit counting algorithm

This question directly follows after reading through Bits counting algorithm (Brian Kernighan) in an integer time complexity . The Java code in question is int count_set_bits(int n) { int count = 0; while(n != 0) { n &= (n-1); count++; } } I want to understand what n &= (n-1) is achieving here ? I have seen a similar kind of construct in another nifty algorithm

请解释Kernighan位计算算法的逻辑

在以整数时间复杂度阅读比特计数算法(Brian Kernighan)之后,此问题直接遵循。 有问题的Java代码是 int count_set_bits(int n) { int count = 0; while(n != 0) { n &= (n-1); count++; } } 我想知道n &= (n-1)在这里实现了什么? 我在另一个漂亮的算法中看到了类似的构造,用于检测数是否是2的幂,如: if(n & (n-1) == 0) { System.out.println("The number is a power of 2");

Sobel operator doesn't work with rectangle images

I try to implement Sobel operator in Java but the result is just some mix of pixels. int i, j; FileInputStream inFile = new FileInputStream(args[0]); BufferedImage inImg = ImageIO.read(inFile); int width = inImg.getWidth(); int height = inImg.getHeight(); int[] output = new int[width * height]; int[] pixels = inImg.getRaster().getPixels(0, 0, width, height, (int[])nul

索贝尔操作符不适用于矩形图像

我尝试在Java中实现Sobel操作符,但结果只是一些像素的混合。 int i, j; FileInputStream inFile = new FileInputStream(args[0]); BufferedImage inImg = ImageIO.read(inFile); int width = inImg.getWidth(); int height = inImg.getHeight(); int[] output = new int[width * height]; int[] pixels = inImg.getRaster().getPixels(0, 0, width, height, (int[])null); double Gx; dou

Applying sobel filter on jpg picture with OpenCV 2.4.10 in Java

I'm trying to add sobel operator on a jpg picture with Java. I found example here: http://www.tutorialspoint.com/java_dip/applying_sobel_operator.htm but it doesn't work. Instead it prints black image. Could someone explain to me what I did wrong, please? Other imgproc functions works well. Here is my code: Mat sourceImage = Highgui.imread(sourcePath, Highgui.CV_LOAD_IMAGE_GRAYSC

在Java中使用OpenCV 2.4.10在jpg图片上应用sobel过滤器

我正在试图在Java上添加一个jpg图片的sobel操作符。 我在这里找到示例:http://www.tutorialspoint.com/java_dip/applying_sobel_operator.htm,但它不起作用。 相反,它会打印黑色图像。 有人可以向我解释我做错了吗? 其他imgproc函数运行良好。 这是我的代码: Mat sourceImage = Highgui.imread(sourcePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat destinationImage = new Mat(sourceImage.rows(), sourceImage

What exactly is Java EE?

I have been doing Java SE for some years now and moving on to Java EE. However I have some trouble understanding some aspects of Java EE. Is Java EE just a specification? What I mean is: Is EJB Java EE? Are EJB/Spring different implementations of Java EE? I am sorry to ask but I have some difficulties to understand what Java EE is. Could someone explain what Java EE is? And EJB? Is Ja

Java EE究竟是什么?

多年来我一直在做Java SE并转向Java EE。 但是,我在理解Java EE的某些方面时遇到了一些麻烦。 Java EE只是一个规范吗? 我的意思是:EJB Java EE是什么? Java EE的EJB / Spring有不同的实现吗? 我很抱歉问,但是我理解什么是Java EE有一些困难。 有人能解释一下Java EE是什么吗? 和EJB? Java EE只是一个规范吗? 我的意思是:EJB Java EE是什么? Java EE的确是一个抽象的规范。 任何人都愿意开发并提供规

Simple way to repeat a String in java

I'm looking for a simple commons method or operator that allows me to repeat some String n times. I know I could write this using a for loop, but I wish to avoid for loops whenever necessary and a simple direct method should exist somewhere. String str = "abc"; String repeated = str.repeat(3); repeated.equals("abcabcabc"); Related to: repeat string javascript Create NSString by repeatin

在java中重复String的简单方法

我正在寻找一种简单的常用方法或运算符,它允许我重复某些字符串n次。 我知道我可以使用for循环来写这个,但是我希望在必要时避免出现循环,并且在某处应该存在一个简单的直接方法。 String str = "abc"; String repeated = str.repeat(3); repeated.equals("abcabcabc"); 相关: 重复字符串javascript通过重复给定次数的另一个字符串来创建NSString 编辑 当它们不是完全必要时,我尽量避免出现循环,因为: 即使它