代码比较多,,,,,这是synchronized块代码package com.blackjack.thread;public class TestSynchronizedBlock {
/**
 * 测试synchronized块
 */
public static void main(String[] args) {
Synchronized sy = new Synchronized();
PThread pt = new PThread(sy);
JThread jt = new JThread(sy);
PThread pt1 = new PThread(sy);
JThread jt1 = new JThread(sy);
                pt.start();
jt.start();
pt1.start();
jt1.start();
}
}
class Synchronized {
private int number;
public void plus() {
synchronized (this) {
while (1 == number) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++;
notify();
System.out.println(number);
}
}
public void jian(){
synchronized (this){
while(0 == number){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
number++;
notify();
System.out.println(number);
}
}
}
class PThread extends Thread{
private Synchronized sy;
public PThread(Synchronized sy) {
this.sy = sy;
}
public void run() {
for(int i = 0;i<10;i++){
sy.plus();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class JThread extends Thread{
private Synchronized sy;
public JThread(Synchronized sy) {
this.sy = sy;
}
public void run() {
for(int i = 0;i<10;i++){
sy.jian();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
这是synchronized修饰方法的代码package com.blackjack.thread;public class TestTogether { /**
 * 测试线程同步,启动多个线程,对一个数字进行+1,-1操作,只出现0,1
 */
public static void main(String[] args) {
Source source = new Source();
PlusThread pt = new PlusThread(source);
JianThread jt = new JianThread(source);
PlusThread pt1 = new PlusThread(source);
JianThread jt1 = new JianThread(source);
pt.start();
jt.start();
pt1.start();
jt1.start();
}
}
class Source{
private int number;//要操作的数
//对该数字进行+1操作
public synchronized void plus(){
while(1 == number){//如果该数字已经进行了+1操作,等待另外一个线程进行-1操作
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++;
System.out.println(this.number);
notify();//通知另外一个线程进行-1操作
}
//对数字进行-1操作
public synchronized void jian(){
while(0 == number){//如果该数字已经进行过-1操作,等待另外一个线程进行+1操作
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number--;
System.out.println(this.number);
notify();//通知另外一个线程进行+1操作
}
}
//+1操作的线程
class PlusThread extends Thread{
private Source source;//持有Source的引用
public PlusThread(Source source) {
this.source = source;
}
public void run() {
for(int i = 0;i<20;i++){
source.plus();
}
}
}
class JianThread extends Thread{
private Source source;
public JianThread(Source source) {
this.source = source;
}
public void run() {
for(int i = 0;i<20;i++){
source.jian();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}为什么第一种情况会出现2,3,4,5,6,,,,,,,不是01010101?谢谢

解决方案 »

  1.   


    package com.blackjack.thread;public class TestSynchronizedBlock {
    /**
     * 测试synchronized块
     */
    public static void main(String[] args) {
    Synchronized sy = new Synchronized();
    PThread pt = new PThread(sy);
    JThread jt = new JThread(sy);
    PThread pt1 = new PThread(sy);
    JThread jt1 = new JThread(sy);
                    pt.start();
    jt.start();
    pt1.start();
    jt1.start();
    }
    }
    class Synchronized {
    private int number;
    public void plus() {
    synchronized (this) {
    while (1 == number) {
    try {
    wait();
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    number++;
    notify();
    System.out.println(number);
    }
    }
    public void jian(){
    synchronized (this){
    while(0 == number){
    try {
    wait();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    number++;
    notify();
    System.out.println(number);
    }
    }
    }
    class PThread extends Thread{
    private Synchronized sy;
    public PThread(Synchronized sy) {
    this.sy = sy;
    }
    public void run() {
    for(int i = 0;i<10;i++){
    sy.plus();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }
    class JThread extends Thread{
    private Synchronized sy;
    public JThread(Synchronized sy) {
    this.sy = sy;
    }
    public void run() {
    for(int i = 0;i<10;i++){
    sy.jian();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    }