当前位置:文档之家› Java多线程题目及答案

Java多线程题目及答案

任务8 多线程编程一、实验目的1. 掌握线程的概念、线程的生命周期。

2. 掌握采用继承Thread 类创建子线程。

3. 掌握使用Runnable 接口使类线程化。

二、实验要求1.掌握线程的概念、线程的生命周期。

2.掌握使用Runnable 接口使类线程化。

三、实验内容一、输入以下源代码,多次运行程序,分析输出结果1. 继承Thread 类创建子线程public class MultiThreadExample{public static void main(String []args){new ThreadDemo("A").start();//启动线程Anew ThreadDemo("B").start();//启动线程B}}class ThreadDemo extends Thread{public ThreadDemo(String n){super(n); //线程名称}public void run(){for(int i=0;i<5;i++){try{// 睡眠一段随机时间Thread.sleep((long)(Math.random() * 1000));}catch(InterruptedException e){e.printStackTrace();}System.out.print(getName()); //打印线程名称}}}2. 使用Runnable 接口使类线程化class MyThread1 implements Runnable { // 实现Runnable接口创建线程类MyThread public void run() { // 实现Runnable接口的run()方法for (int i = 0; i < 9; i++) {System.out.println(Thread.currentThread().getName()+i + " ");}}}public class ThreadExample2 {public static void main(String args[]) {MyThread1 mt = new MyThread1(); // 创建线程类MyThread的实例tThread t = new Thread(mt); // 创建Thread类的实例tt.start(); // 启动线程for (int i = 0; i < 9; i++) {System.out.println(Thread.currentThread().getName()+i + " ");}}}3 多次运行以下程序public class Tst11 implements Runnable {private int x;private int y;public static void main(String[] args) {Tst11 t = new Tst11();new Thread(t).start();new Thread(t).start();}public void run() {for (int i=1;i<20;i++) {try {Thread.sleep(200);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}x++;y++;System.out.println("x=" + x + ",y=" + y);}}}判断以上代码的运行结果是?A 编译不通过B 输出行类似x=1,y=1 ,总是重复一次。

C 输出行类似x=1,y=1 ,递增,每行不重复。

D 输出行类似x=2,y=3 ,x和y的值不一定相等分析结果并给出原因二、编写线程程序1 要求:设计一个线程操作类,要求产生3个线程对象,并可以分别设置三个线程的休眠时间,如下所示:1)线程A,休眠10秒2)线程B,休眠20秒3)线程C,休眠30秒package 线程;class MyThread extends Thread{private int sleeptime;public MyThread(String s) {super(s);}public void setSleepTime(int sleeptime) {this.sleeptime= sleeptime;}public void run() {try {System.out.println(Thread.currentThread().getName()+"进入休眠,休眠时间为:"+sleeptime/1000+"秒");Thread.sleep(sleeptimess);System.out.println(Thread.currentThread().getName()+"休眠结束。

");} catch (InterruptedException e) {// TODO自动生成的 catch 块e.printStackTrace();}}}public class Test1 {public static void main(String[] args) {MyThread A = new MyThread("线程A");A.setSleepTime(10000);MyThread B = new MyThread("线程B");B.setSleepTime(20000);MyThread C = new MyThread("线程C");C.setSleepTime(30000);A.start();B.start();C.start();}}2 编写线程程序,要求:一个线程进行阶乘计算(1!+2!+3!+...100!),每次计算时间随机间隔1-10毫秒;另个线程每隔10毫秒时间读取并线程上个线程的运算结果和计算阶乘,比如1!+2!=3package 线程;class MyThreadTest1 implements Runnable{double sum = 0;int i;public void run() {double a = 1;for (int i = 1; i <= 100; i++) {a *= i;sum += a;int b = (int)(Math.random()*10);try {Thread.sleep(b);} catch (InterruptedException e) {// TODO自动生成的 catch 块e.printStackTrace();}}System.out.println(sum);}}public class Test2 {public static void main(String[] args) {MyThreadTest1 my = new MyThreadTest1();new Thread(my).start();while(my.i<=100) {try {Thread.sleep(10);} catch (InterruptedException e) {// TODO自动生成的 catch 块e.printStackTrace();}System.out.println(my.sum);}}}3 编写2个线程,第一个线程,打印字母A-Z,第二个线程打印数字0-9,要求先打印完数字才能打印字母。

package 线程;class Thread1 extends Thread{public void run() {for(int i = 0;i < 10;i++) {System.out.println(i);}}}class Thread2 extends Thread{public void run() {char zi = 'A';for(int i = 0;i < 26;i++) {System.out.println(zi);zi++;}}}public class Test3 {public static void main(String[] args) {Thread1 t1 = new Thread1();Thread2 t2 = new Thread2();t1.start();try {t1.join();} catch (InterruptedException e) {// TODO自动生成的 catch 块e.printStackTrace();}t2.start();}}4有一个抽奖池,该抽奖池中存放了奖励的金额,该抽奖池用一个数组int[] arr ={10,5,20,50,100,200,500,800,2,80,300};创建两个抽奖箱(线程)设置线程名称分别为“抽奖箱1”,“抽奖箱2”,随机从arr数组中获取奖项元素并打印在控制台上,格式如下:抽奖箱1 又产生了一个 10 元大奖抽奖箱2 又产生了一个 100 元大奖 //.....package 线程;public class Test4 {public static void main(String[] args) {Jang j = new Jang();Thread t3 = new Thread(j,"抽奖箱1");Thread t4 = new Thread(j,"抽奖箱2");t3.start();t4.start();}}class Jang implements Runnable{int[] arr = {10,5,20,50,100,200,500,800,2,80,300};int count = arr.length;boolean [] flag = new boolean[arr.length];public void run() {while(true) {synchronized (this) {if(count>0) {//随机获取数组下标int index = (int)(Math.random()*arr.length);//获取得到的数字int get = arr[index];//代表这张奖票已经抽过if(flag[index]!= true) {flag[index] = true;System.out.println(Thread.currentThread().getName()+"又产生了一个"+get+"元大奖!");count--;}}}}}}5 模拟多个人通过一个山洞。

相关主题