黑马程序员技术交流社区

标题: 基础题,求提供思路 [打印本页]

作者: 浅色寂语66    时间: 2016-8-9 00:04
标题: 基础题,求提供思路
7、 需求:
        (1)双元课堂基础班的学生要进入黑马就业班,必须参加点招笔试和面试,请使用多线程模拟分组的过程
        (2)定义四个线程名称分别为"第一组"、"第二组"、"第三组"、"第四组"
        (3)假设我们班有80为学生,编号为1-80,只需要按照编号分组即可,而且是随机分配,不用平均分配
        (4)开启线程,输出结果如下:
                 *         编号为 17 的学生被分到第四组
                 *         编号为 16 的学生被分到第四组
                 *         编号为 4 的学生被分到第一组
作者: Maroon    时间: 2016-8-9 00:32
package com.heima.luntanplay;

public class Test {
        /*
         *
7、 需求:
        (1)双元课堂基础班的学生要进入黑马就业班,必须参加点招笔试和面试,请使用多线程模拟分组的过程
        (2)定义四个线程名称分别为"第一组"、"第二组"、"第三组"、"第四组"
        (3)假设我们班有80为学生,编号为1-80,只需要按照编号分组即可,而且是随机分配,不用平均分配
        (4)开启线程,输出结果如下:
                 *         编号为 17 的学生被分到第四组
                 *         编号为 16 的学生被分到第四组
                 *         编号为 4 的学生被分到第一组
         */
        public static void main(String[] args) {
                new Thread(new Student("第一组")).start();
                new Thread(new Student("第二组")).start();
                new Thread(new Student("第三组")).start();
                new Thread(new Student("第四组")).start();
        }
}

class Student extends Thread {
        private static int sum = 80;
        private int count = 0;
       
        public Student(String name) {
                super(name);
               
        }

        public void run() {
                while(true) {
                        synchronized (Student.class) {
                                if(sum <= 0){
                                        break;
                                }
                                try {
                                        this.sleep(100);
                                } catch (InterruptedException e) {
                                       
                                        e.printStackTrace();
                                }
                               
                                System.out.println("编号为" + sum-- + "的学生被分到" + getName());
                                count++;
                        }
                }
                System.out.println(getName() + "中共有" + count + "个人");
        }
}
作者: Maroon    时间: 2016-8-9 00:35
package com.heima.luntan;

public class Test {
        /*
         *
7、 需求:
        (1)双元课堂基础班的学生要进入黑马就业班,必须参加点招笔试和面试,请使用多线程模拟分组的过程
        (2)定义四个线程名称分别为"第一组"、"第二组"、"第三组"、"第四组"
        (3)假设我们班有80为学生,编号为1-80,只需要按照编号分组即可,而且是随机分配,不用平均分配
        (4)开启线程,输出结果如下:
                 *         编号为 17 的学生被分到第四组
                 *         编号为 16 的学生被分到第四组
                 *         编号为 4 的学生被分到第一组
         */
        public static void main(String[] args) {
                new Thread(new Student("第一组")).start();
                new Thread(new Student("第二组")).start();
                new Thread(new Student("第三组")).start();
                new Thread(new Student("第四组")).start();
        }
}

class Student extends Thread {
        private static int sum = 80;
        private int count = 0;
       
        public Student(String name) {
                super(name);
               
        }

        public void run() {
                while(true) {
                        synchronized (Student.class) {
                                if(sum <= 0){
                                        break;
                                }
                                try {
                                        this.sleep(100);
                                } catch (InterruptedException e) {
                                       
                                        e.printStackTrace();
                                }
                               
                                System.out.println("编号为" + sum-- + "的学生被分到" + getName());
                                count++;
                        }
                }
                System.out.println(getName() + "中共有" + count + "个人");
        }
}

作者: Frankle    时间: 2016-8-9 12:40
[Java] 纯文本查看 复制代码
package com.thread;

public class Test {
       
        public static void main(String[] args) {
               
                Demo d1 = new Demo();
                Thread t1 = new Thread(d1,"第一组");
                Thread t2 = new Thread(d1,"第二组");       
                Thread t3 = new Thread(d1,"第三组");       
                Thread t4 = new Thread(d1,"第四组");       
               
                t1.start();
                t2.start();
                t3.start();
                t4.start();
        }
}

//实现Runnable接口
class Demo implements Runnable{
       
        private int num = 80;//学生人数,共享资源
       
        @Override
        public void run() {
               
                String groupName = Thread.currentThread().getName();
               
                while(true){
                        synchronized ("") {       
                                if(num > 0){
                                        try {
                                                Thread.sleep(100);
                                                System.out.println("编号为"+ num + "的学生被分到了" + groupName);       
                                                num--;
                                        } catch (InterruptedException e) {
                                                e.printStackTrace();
                                        }                                                       
                                }
                        }
                }
        }       
}





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