1.互斥问题
synchronized关键字,天生互斥,只用这个关键字就能实现互斥。两个线程有一个共同的锁lock,那么其中一个拿到lock,另一个就要等待。模型如下:
thread A:
synchronized(lock){
//do
}
thread B:
synchronized(lock){
//do
}
这两个线程的两个synchronized块中的代码是互斥的。
代码如下:两个类SynchronizeTest / SynchronizeTest1
package spidercsdn;
public class SynchronizeTest implements Runnable{
public Object lock = new Object();
public static SynchronizeTest instance = null;
public Object getLock(){
return lock;
}
public static synchronized SynchronizeTest getInstance() {
if(instance==null)
instance = new SynchronizeTest();
return instance;
}
public void setName() throws InterruptedException{
synchronized(lock){
System.out.println("lock inner");
Thread.sleep(10000);
System.out.println("after sleep,you can print");
}
Thread.sleep(10000);
System.out.println("lock outter");
}
public static void main(String[] args) throws InterruptedException {
new Thread(SynchronizeTest.getInstance()).start();
new Thread(new SynchronizeTest1()).start();
}
@Override
public void run() {
try {
SynchronizeTest.getInstance().setName();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package spidercsdn;
public class SynchronizeTest1 implements Runnable{
public void setName(){
synchronized (SynchronizeTest.getInstance().getLock()) {
System.out.println("inner st1");
}
System.out.println("outter st1");
}
public static void main(String[] args) {
}
@Override
public void run() {
SynchronizeTest1 st1 = new SynchronizeTest1();
st1.setName();
}
}
2.wait notify实现的同步问题
wait notify都要出现在synchronized语句块中,一旦wait,wait后面的语句无论在synchronized里面还是外面,一律全都不执行了,让出cpu。
一旦notify,notify后面的语句照常执行,并且通知wait的线程继续执行,但是,只有notify语句后面的在synchronized块中当语句执行完,wait的线程才继续,而notify语句后面的在synchronized块外的代码无所谓,没有特殊性,谁也不用等他执行完。
代码如下:两个类SynchronizeTest / SynchronizeTest1
package spidercsdn;
public class SynchronizeTest implements Runnable{
public Object lock = new Object();
public static SynchronizeTest instance = null;
public Object getLock(){
return lock;
}
public static synchronized SynchronizeTest getInstance() {
if(instance==null)
instance = new SynchronizeTest();
return instance;
}
public void setName() throws InterruptedException{
synchronized(lock){
System.out.println("11111111");
Thread.sleep(10000);
System.out.println("22222222");
lock.wait();
System.out.println("55555555");
}
System.out.println("66666666");
}
public static void main(String[] args) throws InterruptedException {
new Thread(SynchronizeTest.getInstance()).start();
new Thread(new SynchronizeTest1()).start();
}
@Override
public void run() {
try {
SynchronizeTest.getInstance().setName();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package spidercsdn;
public class SynchronizeTest1 implements Runnable{
public void setName() throws InterruptedException{
synchronized (SynchronizeTest.getInstance().getLock()) {
System.out.println("33333333");
SynchronizeTest.getInstance().getLock().notify();
Thread.sleep(10000);
System.out.println("44444444");
}
Thread.sleep(10000);
System.out.println("7777777");
}
@Override
public void run() {
SynchronizeTest1 st1 = new SynchronizeTest1();
try {
st1.setName();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}