美文网首页
线程yield()方法ThreadSuspend

线程yield()方法ThreadSuspend

作者: 勇者与王者 | 来源:发表于2019-10-10 19:08 被阅读0次
    package multiThread;
    /**
    * 
    * @author qz:
    * @version 创建时间:2019年10月10日 上午10:45:52
    * 
    */
    public class ThreadSuspend {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            final Hero gareen = new Hero();
            gareen.name = "盖伦";
            gareen.hp = 6160;
            gareen.damage = 1;
            
            final Hero teemo = new Hero();
            teemo.name = "提莫";
            teemo.hp = 3000;
            teemo.damage = 1;
            
            final Hero bh = new Hero();
            bh.name = "赏金猎人";
            bh.hp= 5000;
            bh.damage = 1;
            
            final Hero leesin = new Hero();
            leesin.name = "李青";
            leesin.hp = 3000;
            leesin.damage = 1;
            
            Thread t1 = new Thread() {
                public void run() {
                    while (!teemo.isDead()) {
                        gareen.attackHero(teemo);
                    }
                }
            };
            
            Thread t2 = new Thread() {
                public void run() {
                    while (!leesin.isDead()) {
                        //临时暂停
                        Thread.yield();
                        bh.attackHero(leesin);
                    }
                }
            };
            
            t1.setPriority(5);
            t2.setPriority(5);
            t1.start();
            t2.start();
            
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:线程yield()方法ThreadSuspend

          本文链接:https://www.haomeiwen.com/subject/kewapctx.html