黑马程序员技术交流社区

标题: 谁来做一做线程题目? [打印本页]

作者: zhucong1    时间: 2016-12-5 23:17
标题: 谁来做一做线程题目?
        定义一个数组String str[] = {"世界杯", "亚洲杯", "欧洲杯", "美洲杯", "奥布莱恩杯"};
        创建两个线程名称分别为"恒大","鲁能" . 随机从str数组中取出元素与线程名称进行组合,在控制台
        输出10次.(输出格式为:"当前线程名称" + "捧起了" + str中的元素)?



作者: dingyilin    时间: 2016-12-12 23:48
我不会楼主教我
作者: miaoyi    时间: 2016-12-13 12:19
[Java] 纯文本查看 复制代码
package com.exercise.ss;

import java.util.Random;

public class Test7 {

        /**
         * @param args
         *
         */
        public static void main(String[] args) {
                myRunnable mr = new myRunnable();
                Thread th1 = new Thread(mr);
                Thread th2 = new Thread(mr);
                th1.setName("恒大");
                th2.setName("鲁能");
                th1.start();
                th2.start();
        }

}
class myRunnable implements Runnable{
        private String str[] ={"世界杯" , "亚洲杯" ,"欧洲杯" , "美洲杯" , "奥布莱恩杯"};
        private int num = 1;
        private Random r = new Random();
        @Override
        public void run() {
                        while(num <= 10){
                                synchronized (this) {
                                        int i = r.nextInt(5);
                                        System.out.println(Thread.currentThread().getName() + "捧起了" + str);
                                        num++;
                                }
                }
               
        }
       
}


作者: zhucong1    时间: 2016-12-13 22:48
miaoyi 发表于 2016-12-13 12:19
[mw_shl_code=java,true]package com.exercise.ss;

import java.util.Random;

可以,给你点个赞
作者: guyouzhong    时间: 2017-1-27 21:29
有点简单

作者: lvshen9    时间: 2017-2-18 13:59
本帖最后由 lvshen9 于 2017-2-18 14:02 编辑

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test2_1 {
        public static void main(String[] args) {
               final String str[] = {"世界杯", "亚洲杯", "欧洲杯", "美洲杯", "奥布莱恩杯"};  
                final Test2_1 test=new Test2_1();
                Thread t1=new Thread(){
                        public void run(){
                                test.myRun(str);
                        }
                };
                Thread t2=new Thread(){
                        public void run(){
                                test.myRun(str);
                        }
                };              
                t1.setName("恒大");
                t2.setName("鲁能");              
                t1.start();
                t2.start();
        }
   
        private Random r=new Random();
        private Lock lock=new ReentrantLock();
     public void myRun(String[] str){
                for(int num=0;num<10;num++){
                        
                        lock.lock();
                        try {
                                int i=r.nextInt(5);
                                System.out.println(Thread.currentThread().getName()+"捧起了"+str);
                                
                        } catch (Exception e) {
                                // TODO: handle exception
                        }finally{
                        lock.unlock();
                        }
                }
               
        }
}[/mw_shl_code]





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2