求解答
2 个解决方案
#1
会的,一个线程在没有获取到CPU的时间片的时候,就进入等待队列,等待再次获得cpu时间片进行执行
#2
对于一个已经竞争到同步锁的线程,在还没有走出同步块的时候,即使时间片结束也不会释放锁。
另外,对于通过调用sleep或wait,notify方法主动挂起线程的情况:
通过sleep方式也是不会释放同步锁的,而wait,notify是会释放锁的,
通过下面一个简单的例子可以看出:
调用sleep()方法的时候,输出情况是:
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
而调用wait,notify,输出情况是:
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
另外,对于通过调用sleep或wait,notify方法主动挂起线程的情况:
通过sleep方式也是不会释放同步锁的,而wait,notify是会释放锁的,
通过下面一个简单的例子可以看出:
public class TestThread {
private byte[] tlock = new byte[0];
public void testSyn() {
Thread t1 = new Thread(new TestRunable(tlock));
Thread t2 = new Thread(new TestRunable(tlock));
t1.start();
t2.start();
}
public static void main(String[] args) {
new TestThread().testSyn();
}
class TestRunable implements Runnable{
private byte[] lock;
public TestRunable(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized(lock){
for(int i=0;i<10;i++) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
/*lock.notify();
if(i<9) {
lock.wait();
}*/
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
调用sleep()方法的时候,输出情况是:
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
而调用wait,notify,输出情况是:
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
#1
会的,一个线程在没有获取到CPU的时间片的时候,就进入等待队列,等待再次获得cpu时间片进行执行
#2
对于一个已经竞争到同步锁的线程,在还没有走出同步块的时候,即使时间片结束也不会释放锁。
另外,对于通过调用sleep或wait,notify方法主动挂起线程的情况:
通过sleep方式也是不会释放同步锁的,而wait,notify是会释放锁的,
通过下面一个简单的例子可以看出:
调用sleep()方法的时候,输出情况是:
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
而调用wait,notify,输出情况是:
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
另外,对于通过调用sleep或wait,notify方法主动挂起线程的情况:
通过sleep方式也是不会释放同步锁的,而wait,notify是会释放锁的,
通过下面一个简单的例子可以看出:
public class TestThread {
private byte[] tlock = new byte[0];
public void testSyn() {
Thread t1 = new Thread(new TestRunable(tlock));
Thread t2 = new Thread(new TestRunable(tlock));
t1.start();
t2.start();
}
public static void main(String[] args) {
new TestThread().testSyn();
}
class TestRunable implements Runnable{
private byte[] lock;
public TestRunable(byte[] lock) {
this.lock = lock;
}
@Override
public void run() {
synchronized(lock){
for(int i=0;i<10;i++) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
/*lock.notify();
if(i<9) {
lock.wait();
}*/
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
调用sleep()方法的时候,输出情况是:
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
Thread-1
而调用wait,notify,输出情况是:
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1