回 帖 发 新 帖 刷新版面

主题:关于多线程编程的几个问题,急救!!!请大家帮忙



class ST extends Thread
{
    public ST (String str){super(str);}
    public void run()
    {
        String A[]={"起跳","投篮","篮板"};
        String B[]={"加速","断球","快攻"};
        String C[]={"运球","盖帽","三分"};
        String Name=null;
        Name=getName();
        for(int i=0;i<3;i++)
        {
            if(Name.equals("A"))
        {
            System.out.println(Name+":"+A[i]);
            try
            {sleep((long)(Math.random()*1000));
            }catch(InterruptedException e){}
        }
            if(Name.equals("B"))
        {
            System.out.println(Name+":"+B[i]);
            try
            {sleep((long)(Math.random()*1000));
            }catch(InterruptedException e){}
        }
        if(Name.equals("C"))
        {
            System.out.println(Name+":"+C[i]);
            try
            {sleep((long)(Math.random()*1000));
            }catch(InterruptedException e){}
        }
      }
    }
}
public class Thread
{
    public static void main(String args[])
    {
        new ST("A").start();
        new ST("B").start();
        new ST("C").start();
    }
}
怎样设置三个线程的优先级(一个线程的优先级是最小的,另外的两个为最大,先启动优先级小的线程,然后启动另外的两个。)?
程序哪出了问题?
一个模拟两个人同时向一个账户存款过程的程序(利用两个线程完成同时存款动作;存款过程可以模拟成两个环节“读取帐户余额”、“设置帐户”),怎么实现??

回复列表 (共2个回复)

沙发

[quote]{怎样设置三个线程的优先级(一个线程的优先级是最小的,另外的两个为最大,先启动优先级小的线程,然后启动另外的两个。)?[/quote]

Don't do anything depend on 优先级, because 优先级 is OS dependent, it might/might not work.

板凳

[quote]一个模拟两个人同时向一个账户存款过程的程序(利用两个线程完成同时存款动作;存款过程可以模拟成两个环节“读取帐户余额”、“设置帐户”),怎么实现??[/quote]

This is a classical thread/synchronized/lock/unlock problem.
Do a google search, you many find many existing examples.

我来回复

您尚未登录,请登录后再回复。点此登录或注册