请详细解释<<和<< =以及>>和>> =之间的区别.我知道转换运算符是如何工作的,但是当谈到>> =或<< =时,我对它们并不那么肯定.
我为生产者和消费者实现了一个信号量类.
它工作正常,但我现在感觉我们使用notifyAll来通知唤醒或通知所有线程的线程.
我想知道是否有任何方式我们只能通知一组特定的组:生成器只使用组或其他任何方式通知消费者线程,反之亦然.
PS:我是java和Threading的新手.
我试图理解Java中的Synchornized.我知道如果我从2个不同的线程访问同一个对象的同步方法,一次只能有一个访问.
但我认为如果在2个不同的实例上调用相同的方法,则两个对象都应该能够并行访问该方法.如果从方法访问/修改静态成员变量,这将导致竞争条件.但我无法在下面的代码中看到竞争状况.
有人可以解释一下代码或我的理解是否错误.
有关参考代码,请访问:http://ideone.com/wo6h4R
class MyClass
{
public static int count=0;
public int getCount()
{
System.out.println("Inside getcount()");
return count;
}
public synchronized void incrementCount()
{
count=count+1;
}
}
class Ideone
{
public static void main(String[] args) throws InterruptedException {
final MyClass test1 = new MyClass();
final MyClass test2 = new MyClass();
Thread t1 = new Thread() {
public void run()
{
int k=0;
while (k++<50000000)
{
test1.incrementCount();
}
}
};
Thread …Run Code Online (Sandbox Code Playgroud)