赞
踩
刚开始学多线程编程,课上也没认真听,课下去网上找了PPT,又认真的学了一遍。
问题:
问题①
Animal 类:
- public class Animal extends Thread{
- public Animal(String name){
- super(name);
- }
- public void run(){
- for(int i=0;i<=100;i+=10){
- if(this.getName().equals("乌龟"))
- System.out.println("乌龟跑了 "+ i + " 米");
- else
- System.out.println("兔子跑了 "+ i + " 米");
- }
- try{
- Thread.sleep((long)(Math.random()*1000));
- }
- catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
测试类:
- public class Quetion1 {
-
- public static void main(String[] args) {
- Animal rabbit = new Animal("兔子");
- Animal tortoise = new Animal("乌龟");
- rabbit.start();
- tortoise.start();
- }
- }
问题②
Animal 类:
- public class Animal implements Runnable{
- private String name;
- public Animal(String name){
- this.name=name;
- }
- public String getName(){
- return this.name;
- }
- public void run(){
- for(int i=0;i<=100;i+=10){
- if(this.getName().equals("乌龟"))
- System.out.println("乌龟跑了 "+ i + " 米");
- else
- System.out.println("兔子跑了 "+ i + " 米");
- }
- try{
- Thread.sleep((long)(Math.random()*1000));
- }
- catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
测试类:
- public class Quetion2 {
-
- public static void main(String[] args) {
- Animal rabbit = new Animal("兔子");
- Animal tortoise = new Animal("乌龟");
- Thread t1 = new Thread(rabbit);
- Thread t2 = new Thread(tortoise);
- t1.start();
- t2.start();
- }
- }
问题③
Animal 类:
- public class Animal extends Thread{
-
- public Animal(String name) {
- super(name);
- }
-
- public void run() {
- for(int i=0;i<=100;i+=10) {
- if(this.getName().equals("乌龟"))
- System.out.println("乌龟跑了 " + i + "米");
- else
- System.out.println("兔子跑了 " + i + "米");
- }
- }
- }
测试类:
- public class Question3 {
-
- public static void main(String[] args) {
- Animal rabbit = new Animal("兔子");
- Animal tortoise = new Animal("乌龟");
- rabbit.setPriority(1);
- tortoise.setPriority(10);
- rabbit.start();
- tortoise.start();
- }
-
- }
问题④
Animal 类:
- public class Animal implements Runnable{
-
- Thread rabbit,tortoise;
-
- public Animal() {
- rabbit = new Thread(this,"兔子");
- tortoise = new Thread(this,"乌龟");
- }
-
- public void run() {
- if(Thread.currentThread() == tortoise) {
-
- System.out.println("此时兔子在休眠,被乌龟吵醒。乌龟说:该你去跑步了!");
-
- rabbit.interrupt();
-
- System.out.println("乌龟继续跑步...");
- }
- else {
- try{
- System.out.println("兔子正在休眠...乌龟正在跑步");
- rabbit.sleep(1000*60);
- }
- catch(InterruptedException e) {
- System.out.println("兔子被叫醒");
- System.out.println("兔子开始跑步");
- }
- }
- }
- }
测试类:
- public class Question4 {
-
- public static void main(String[] args) {
-
- Animal animal = new Animal();
- System.out.println("开始状态:");
- System.out.println("兔子的线程状态: " + animal.rabbit.getState());
- System.out.println("乌龟的线程状态: " + animal.tortoise.getState());
- System.out.println("比赛开始:");
- animal.rabbit.start();
- animal.tortoise.start();
- System.out.println("兔子的线程状态: " + animal.rabbit.getState());
- System.out.println("乌龟的线程状态: " + animal.tortoise.getState());
- System.out.println("当前活动总线程数: " + Thread.activeCount());
- /*这里输出总线程数位3,因为除了乌龟和兔子还有一个主线程*/
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。