java - 为什么 int[] a = new int[0]; 允许吗?

分类: bt365体育 发布时间: 2025-07-18 07:47:40 作者: admin 阅读: 8210

An empty array can be used in thread synchronization when the goal is to use the least amount of memory for a locking object. Recall that arrays are objects, so if you wish to synchronize multiple threads on a single dummy object, the smallest object you can utilize is an empty array (possibly byte size):

byte bLock = new byte[0];

// Thread T1 synchronizes on this empty array object

synchronize(bLock) {

// perform some task while blocking other threads

// synchronizing on bLock

}