当前位置:首页 > Java多线程题目及答案
任务8 多线程编程
一、实验目的
1. 掌握线程的概念、线程的生命周期。 2. 掌握采用继承Thread 类创建子线程。 3. 掌握使用Runnable 接口使类线程化。 二、实验要求
1.掌握线程的概念、线程的生命周期。 2.掌握使用Runnable 接口使类线程化。 三、实验内容
一、输入以下源代码,多次运行程序,分析输出结果
1. 继承Thread 类创建子线程 public class MultiThreadExample{ public static void main(String []args){ new ThreadDemo(\).start();//启动线程A new ThreadDemo(\).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的实例t Thread t = new Thread(mt); // 创建Thread类的实例t t.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 block e.printStackTrace(); } x++; y++; System.out.println(\ + x + \ + 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()+\进入休眠,休眠时间为:\秒\ 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!=3
package 线程;
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); } } }
共分享92篇相关文档