blusewang
2013-06-02 11:27
浏览 345
已采纳

java 并发 synchronized问题

上码:

 

package com.hxsmart.thread.synchronize;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class CleanRoom {
    private Integer student=0;
    private Integer machine=0;
    
    public String getDateStr(){
        return "  Thread"+Thread.currentThread().getName()+"  *time: "+new SimpleDateFormat("mm:ss").format(new Date());
    }
    public void stuClean(){
        System.out.println("enter stuClean");
        synchronized (student) {
            for(int i=0;i<5;i++){
                System.out.println("stu"+getDateStr());
                student++;
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public void macClean(){
        System.out.println("enter macClean");
        synchronized (machine) {
            for(int i=0;i<5;i++){
                System.out.println("mac"+getDateStr());
                machine++;
//              try {
//                  TimeUnit.SECONDS.sleep(3);
//              } catch (InterruptedException e) {
//                  e.printStackTrace();
//              }
            }
        }
    }
    public static void main(String[] args) {
        CleanRoom c=new CleanRoom();
        
        StuThread t1=new StuThread(c);
        MacThread t2=new MacThread(c);
        
        t1.start();
        t2.start();
    }
    public Integer getStudent() {
        return student;
    }
    public void setStudent(Integer student) {
        this.student = student;
    }
    public Integer getMachine() {
        return machine;
    }
    public void setMachine(Integer machine) {
        this.machine = machine;
    }
    
}

class StuThread extends Thread{
    CleanRoom clean;
    public StuThread() {
        super();
    }
    public StuThread(CleanRoom clean) {
        super();
        this.clean = clean;
    }
    @Override
    public void run() {
        //for(int i=0;i<5;i++)
        clean.stuClean();
        System.out.println("stu: "+clean.getStudent()+"**  mac:**"+clean.getMachine());
    }
    public CleanRoom getClean() {
        return clean;
    }
    public void setClean(CleanRoom clean) {
        this.clean = clean;
    }
    
}
class MacThread extends Thread{
    CleanRoom clean;
    public MacThread() {
        super();
    }
    public MacThread(CleanRoom clean) {
        super();
        this.clean = clean;
    }
    @Override
    public void run() {
        //for(int i=0;i<5;i++)
        clean.macClean();
        System.out.println("stu: "+clean.getStudent()+"**  mac:**"+clean.getMachine());
    }
    public CleanRoom getClean() {
        return clean;
    }
    public void setClean(CleanRoom clean) {
        this.clean = clean;
    }
}

 输出:

 

 

enter stuClean
enter macClean
stu  ThreadThread-0  *time: 12:38
stu  ThreadThread-0  *time: 12:41
stu  ThreadThread-0  *time: 12:44
stu  ThreadThread-0  *time: 12:47
stu  ThreadThread-0  *time: 12:50
stu: 5**  mac:**0
mac  ThreadThread-1  *time: 12:53
mac  ThreadThread-1  *time: 12:53
mac  ThreadThread-1  *time: 12:53
mac  ThreadThread-1  *time: 12:53
mac  ThreadThread-1  *time: 12:53
stu: 5**  mac:**5

 问题:

 

t1和t2线程,分别调用的是CleanRoom类同一实例c的不同方法,并且这两个方法没有把这个实例C锁上,两个方法所锁定的资源分别是c实例的两个不同的属性,按道理来说,这两个线程应该是并发执行的。

但是事实不如此,t2线程在t1线程没有执行完成前,始终处于等待状态。

 

求解!

当stuClean方法中调用TimeUnit.SECONDS.sleep(3)后,macClean()方法应该有机会得到执行啊?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题