第一章-对象和封装
上机练习1-5答案略
简答题4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Student1 { String name; private int age;
public int getAge() { return age; }
public void setAge(int age) { if (age>=16) { this.age = age; } else { System.out.println("输入错误!"); } }
public void print() { System.out.println("该学生姓名为:" + name + ",年龄为:" + this.getAge()); } }
class Student1Test { public static void main(String[] args) { Student1 s = new Student1(); s.name = "小黑"; s.setAge(10); s.print();
Student1 s1 = new Student1(); s1.name = "小白"; s1.setAge(16); s1.print(); } }
|
简答题5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| public class Student2 { String name; int age; String sex; String subject;
public void print() { System.out.println("该学生姓名为:" + name + ",年龄为:" + age + ",性别为:" + sex + ",专业是:" + subject); }
public Student2(String name,int age) { this.sex = "男"; this.subject = "ACCP"; this.name = name; this.age = age; this.print(); }
public Student2(String name,int age,String sex,String subject) { this.name = name; this.age = age; this.sex = sex; this.subject = subject; this.print(); }
} class Student2Test { public static void main(String[] args) { Student2 s1 = new Student2("张三", 18); Student2 s2 = new Student2("李四", 18,"男","计算机"); } }
|
第二章-继承
上机练习1
宠物类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.lx1;
public class Pet { private String name; private int health; private int love;
public String getName() { return name; }
public int getHealth() { return health; }
public int getLove() { return love; }
public void print() { System.out.println("宠物名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove()); }
public Pet() {}
public Pet(String name) { this.name = name; this.health = 100; this.love = 50; } }
|
狗类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.lx1;
public class Dog extends Pet{ private String strain;
public String getStrain() { return strain; }
public Dog(String name, String strain) { super(name); this.strain = strain; }
@Override public void print() { System.out.println("名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove() + ",简介为:" + getStrain()); } }
|
企鹅类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.lx1;
public class Penguin extends Pet{ private String sex;
public String getSex() { return sex; }
public Penguin(String name,String sex) { super(name); this.sex = sex; }
@Override public void print() { System.out.println("名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove() + ",性别为:" + getSex()); } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.lx1;
public class Test { public static void main(String[] args) { Pet pet = new Pet("贝贝"); pet.print();
Dog d = new Dog("小黑","聪明的拉布拉多"); d.print();
Penguin p = new Penguin("小白","Q妹"); p.print(); } }
|
上机练习2
父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.lx2;
public abstract class Pet { private String name; private int health; private int love;
public String getName() { return name; }
public int getHealth() { return health; }
public int getLove() { return love; }
public abstract void print();
public Pet() {}
public Pet(String name) { this.name = name; this.health = 100; this.love = 50; } }
|
子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.lx2;
public class Dog extends Pet { private String strain;
public String getStrain() { return strain; }
public Dog(String name, String strain) { super(name); this.strain = strain; }
@Override public void print() { System.out.println("名字是:" + getName() + ",健康值为:" + getHealth() + ",亲密度为:" + getLove() + ",简介为:" + getStrain()); } }
|
测试类
1 2 3 4 5 6 7 8 9 10
| package com.lx2;
public class Test { public static void main(String[] args) { Dog d = new Dog("小黑","聪明的拉布拉多"); d.print(); } }
|
综合练习
汽车类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.zh;
public abstract class MotoVehicle { private String no; private String brand;
public MotoVehicle() {}
public MotoVehicle(String no, String brand) { this.no = no; this.brand = brand; }
public abstract int calRent(int days); }
|
客车类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.zh;
public final class Bus extends MotoVehicle { private int seatCount; public Bus() {} public Bus(String no, String brand, int seatCount) { super(no, brand); this.seatCount = seatCount; }
public int calRent(int days) { if (seatCount <= 16) { return days * 800; } else { return days * 1500; } } }
|
轿车类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.zh;
public final class Car extends MotoVehicle { private String type; public Car() {} public Car(String no, String brand, String type) { super(no, brand); this.type = type; }
public int calRent(int days) { if ("1".equals(type)) { return days * 500; } else if ("2".equals(type)) { return 600 * days; } else { return 300 * days; } } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package com.zh;
import java.util.Scanner;
public class TestRent { public static void main(String[] args) { String no; String brand;
int money;
Scanner input = new Scanner(System.in); System.out.println("欢迎您来到汽车租赁公司!");
System.out.print("请输入要租赁的天数:"); int days = input.nextInt();
System.out.print("请输入要租赁的汽车类型(1:轿车\t2、客车):"); String mtype = input.next();
if("1".equals(mtype)){ System.out.print("请输入要租赁的汽车品牌(1、宝马\t2、别克):"); brand = input.next(); System.out.print("请输入轿车的型号:"); if("1".equals(brand)) { System.out.print("1、550i:"); } else { System.out.print("2、商务舱GL8\t3、林荫大道"); } String type = input.next(); no = "京BK5543"; System.out.println("分配给您的汽车牌号是:" + no); Car c = new Car(no,brand,type); money = c.calRent(days); } else { System.out.print("请输入要租赁的客车品牌(1、金杯\t2、金龙):"); brand = input.next(); System.out.print("请输入客车的座位数:"); int seatCount = input.nextInt(); no = "京AU8769"; System.out.println("分配给您的汽车牌号是:" + no); Bus b = new Bus(no,brand,seatCount); money = b.calRent(days); } System.out.println("\n顾客您好!您需要支付的租赁费用是" + money + "。"); } }
|
简答题5
动物类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.jd5;
public abstract class Animal { private int age;
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public Animal(int age) { setAge(age); }
public abstract void info(); }
|
鸟类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.jd5;
public class Bird extends Animal{ private String color;
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public Bird(int age, String color) { super(age); setColor(color); }
@Override public void info() { System.out.println("我是一只" + getColor() + "的鸟"); System.out.println("我今年" + super.getAge() + "岁了!"); } }
|
鱼类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.jd5;
public class Fish extends Animal{ private String weight;
public String getWeight() { return weight; }
public void setWeight(String weight) { this.weight = weight; }
public Fish(int age, String weight) { super(age); setWeight(weight); }
@Override public void info() { System.out.println("我是一只" + getWeight() + "的鱼"); System.out.println("我今年" + super.getAge() + "岁了!"); } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.jd5;
public class Test { public static void main(String[] args) { Bird b = new Bird(4,"红色"); b.info();
System.out.println(); Fish f = new Fish(2,"五斤重"); f.info(); } }
|
第三章-多态
简答题5
1 2 3 4 5 6 7 8
| package com.jd5;
public abstract class Printer {
public abstract void print(); }
|
1 2 3 4 5 6 7 8 9
| package com.jd5;
public class InkpetPrinter extends Printer{ @Override public void print() { System.out.println("这是喷墨打印机。"); } }
|
1 2 3 4 5 6 7 8 9 10
| package com.jd5;
public class DotMatrixPrinter extends Printer{
@Override public void print() { System.out.println("这是针式打印机。"); } }
|
1 2 3 4 5 6 7 8 9
| package com.jd5;
public class LaserPrinter extends Printer{ @Override public void print() { System.out.println("这是激光打印机。"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.jd5;
public class Test { public static void main(String[] args) { Printer p1 = new DotMatrixPrinter(); p1.print(); Printer p2 = new InkpetPrinter(); p2.print(); Printer p3 = new LaserPrinter(); p3.print(); } }
|
简答题6
朋友类,作为父类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.jd6;
public abstract class Friend { private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Friend(String name) { setName(name); }
public abstract void eat();
public abstract void play(); }
|
朋友1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.jd6;
public class Fd1 extends Friend{ public Fd1(String name) { super(name); }
@Override public void eat() { System.out.println(getName()); System.out.print("给你做披萨。"); }
@Override public void play() { System.out.println("去打橄榄球。"); } }
|
朋友2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.jd6;
public class Fd2 extends Friend{
public Fd2(String name) { super(name); }
@Override public void eat() { System.out.println(getName()); System.out.print("给你做四川凉菜。"); }
@Override public void play() { System.out.print("去练太极拳。"); } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.jd6;
public class Test { public static void main(String[] args) { Friend f1 = new Fd1("约翰"); Friend f2 = new Fd2("王小强");
f1.eat(); f1.play(); System.out.println();
f2.eat(); f2.play(); } }
|
第四章-接口
静等更新~~ ~~
第五章-异常
上机练习1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.lx1;
import java.util.InputMismatchException; import java.util.Scanner;
public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("请输入课程代号(1~3之间数字):");
try { int num = sc.nextInt(); switch (num) { case 1: System.out.println("C#编程"); break; case 2: System.out.println("Java编程"); break; case 3: System.out.println("MySQL编程"); break; default: System.out.println("输入有误!"); break; } } catch (InputMismatchException x) { System.out.println("请输入数字!"); return; } catch (Exception x) { System.out.println("未知异常!"); }
System.out.println("欢迎提出建议!"); } }
|
上机练习2
用户类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.lx2;
public class Person { private String name; private int age; private String sex = "女";
public void setAge(int age) throws Exception { if (!(age < 1 || age > 100)) { this.age = age; } else { throw new Exception("年龄必须在1~100之间!"); } }
public void print() { System.out.println("姓名是:" + this.name + "年龄是:" + this.age + "性别是:" + this.sex); } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.lx2;
import org.apache.log4j.Logger;
public class TestException { public static void main(String[] args) { Logger log = Logger.getLogger(TestException.class); Person p = new Person(); try { p.setAge(120); p.print(); } catch (Exception x) { x.printStackTrace(); log.error("输入有误!"); } } }
|
上机练习3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.lx3;
import org.apache.log4j.Logger;
import java.util.InputMismatchException; import java.util.Scanner;
public class TestLog1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Logger log = Logger.getLogger(TestLog1.class); try { System.out.print("请输入被除数:"); int num1 = sc.nextInt(); System.out.print("请输入除数:"); int num2 = sc.nextInt(); System.out.println("结果为:" + (num1 / num2)); } catch (ArithmeticException e) { log.warn("非法参数");
e.printStackTrace(); } catch (InputMismatchException e) { throw new InputMismatchException("请输入整数!"); } catch (Exception e) { System.out.println("未知错误!"); } finally { System.out.println("感谢您使用本程序!"); } } }
|
简答题3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.jd3;
public class TestJd3 { public static void main(String[] args) { int[] nums = {1,2,3,4,5};
try { for (int i = 0; i <= nums.length; i++) { System.out.print(nums[i] + "\t"); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println("\n数组下标越界异常。"); e.printStackTrace(); } } }
|
简答题5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.jd5;
import org.apache.log4j.Logger;
public class TestJd5 { public static void main(String[] args) { Logger log = Logger.getLogger(TestJd5.class); int[] nums = {1,2,3,4,5};
try { for (int i = 0; i <= nums.length; i++) { System.out.print(nums[i] + "\t"); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println(); log.error("数组下标越界异常!"); e.printStackTrace(); } } }
|
第六章-集合框架
企鹅类统一代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.lx1;
public class Penguin { private String name; private String strain;
public Penguin(String name, String strain) { setName(name); setStrain(strain); }
public void print() { System.out.println("我叫:" + name + "简介是:" + strain); }
public void setName(String name) { this.name = name; }
public void setStrain(String strain) { this.strain = strain; } }
|
上机练习1
企鹅类引自上文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.lx1;
import java.util.ArrayList; import java.util.List;
public class Test { public static void main(String[] args) { List<Penguin> list = new ArrayList<Penguin>();
Penguin p1 = new Penguin("小白","可爱的小白"); Penguin p2 = new Penguin("小黑","可爱的小黑"); Penguin p3 = new Penguin("小黄","可爱的小黄");
list.add(p1); list.add(p2); list.add(p3);
System.out.println("企鹅的数量是:" + list.size());
for (Penguin p : list) { p.print(); } System.out.println();
list.remove(0); for (Penguin p : list) { p.print(); } System.out.println();
if (list.contains(p3)) { System.out.println("包含此企鹅!"); } else { System.out.println("抱歉。不包含此企鹅!"); } } }
|
上机练习2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.lx2;
import java.util.*;
public class Test { public static void main(String[] args) { Map<String, String> m = new HashMap<String, String>(); Scanner sc = new Scanner(System.in);
m.put("小白","聪明的小白"); m.put("小黑","聪明的小黑"); m.put("小黄","聪明的小黄");
System.out.print("请输入宠物昵称:"); String name = sc.next();
if (m.containsKey(name)) { System.out.println(m.get(name)); } else { System.out.println("抱歉。不包含此宠物!"); } } }
|
上机练习3
企鹅类引自上文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.lx3;
import java.util.*;
public class Test { public static void main(String[] args) { Map<String,String> penguin = new HashMap<String,String>(); penguin.put("小白", "可爱的小白"); penguin.put("小黑", "可爱的小黑"); penguin.put("小黄", "可爱的小黄"); Set<String> keys = penguin.keySet(); Iterator<String> it = keys.iterator(); System.out.println("宠物信息是:"); while(it.hasNext()){ String key = it.next(); String value = penguin.get(key); System.out.println(key + "\t" + value); } } }
|
上机练习4
企鹅类引自上文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.lx4;
import java.util.*;
public class Test { public static void main(String[] args) { Map<String, String> penguin = new HashMap<String, String>(); penguin.put("小白", "可爱的小白"); penguin.put("小黑", "可爱的小黑"); penguin.put("小黄", "可爱的小黄"); Set<String> keys = penguin.keySet(); Iterator<String> it = keys.iterator(); System.out.println("使用iterator遍历宠物信息是:"); int num = 0; while (it.hasNext()) { String key = it.next(); String value = penguin.get(key); System.out.println((num + 1) + key + "\t" + value); num++; }
System.out.println(); System.out.println("用foreach遍历宠物信息是:"); num = 0; for (String key : keys) { String value = penguin.get(key); System.out.println((num + 1) + key + "\t" + value); num++; }
} }
|
简答题5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.jd5;
import java.util.*;
public class TestJd5 { public static void main(String[] args) { Map<String, Integer> m = new HashMap<String, Integer>(); Scanner sc = new Scanner(System.in); m.put("张三", 75); m.put("李四", 85); m.put("王五", 50);
System.out.println(m.keySet()); System.out.println(m.values());
System.out.print("请输入学员昵称:"); String name = sc.next();
if (m.containsKey(name)) { System.out.print("请输入要修改的成绩是:"); int score = sc.nextInt(); m.put(name,score); System.out.println(m.keySet()); System.out.println(m.values()); } else { System.out.println("抱歉。没有此成员信息!"); }
} }
|
第七章-多线程
上机练习1
需求说明
创建两个线程。每个线程均输出20次信息文字,”你好“,线程名。并观察多个线程交替执行的过程,运行结果如图所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.lx1;
public class MyThread extends Thread{
@Override public void run() { for (int i = 0; i < 20; i++) { System.out.println((i + 1) + ",你好,来自线程" + Thread.currentThread().getName()); } }
public static void main(String[] args) { MyThread t = new MyThread(); t.start();
MyThread t2 = new MyThread(); t2.start(); } }
|
上机练习2
需求说明
修改上机练习1,要求线程类使用实现Runnable接口的方式创建,并修改测试类。运行结果如上机练习1所示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.lx2;
public class MyThread implements Runnable{
@Override public void run() { for (int i = 0; i < 20; i++) { System.out.println((i + 1) + ",你好,来自线程" + Thread.currentThread().getName()); } }
public static void main(String[] args) { MyThread m = new MyThread(); Thread t = new Thread(m); Thread t2 = new Thread(m);
t.start(); t2.start(); } }
|
上机练习4
需求说明
任意创建一个线程类,在测试类中创建子程序对象,显示主线程、子线程默认优先级,将主线程设置为最高优先级、子线程设置为最低优先级并显示。运行结果如图所示:
线程类
1 2 3 4 5 6 7 8
| package com.lx4;
public class ThreadMe extends Thread{
@Override public void run() {} }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.lx4;
public class Test { public static void main(String[] args) { ThreadMe td1 = new ThreadMe(); Thread t = Thread.currentThread();
System.out.println("--------显示默认优先级--------"); System.out.println("主线程名:" + t.getName() + ",优先级:" + t.getPriority()); System.out.println("子线程名:" + td1.getName() + ",优先级:" + td1.getPriority());
t.setPriority(Thread.MAX_PRIORITY); td1.setPriority(Thread.MIN_PRIORITY);
System.out.println("--------修改默认优先级后--------"); System.out.println("主线程名:" + t.getName() + ",优先级:" + t.getPriority()); System.out.println("子线程名:" + td1.getName() + ",优先级:" + td1.getPriority()); } }
|
上机练习5
需求说明
某医院设置有普通号和特需号,假设某科室一天需看普通号50个、特需号10个,每个特需号的看病时间约是普通号的两倍,开始时普通号和特需号并行叫号,叫到特需号的概率比普通号高,当普通号叫完第10号时,要求先看完全部特需号,再看普通号,请使用多线程模拟这一过程,运行结果如图所示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.lx5;
public class Test extends Thread{
@Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.err.println("特需号:" + (i + 1) + "号病人正在看病!"); } }
public static void main(String[] args) { Test t = new Test(); t.setPriority(Thread.MAX_PRIORITY); t.start();
for (int i = 0; i < 50; i++) { if (i == 10) { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("普通号:" + (i + 1) + "号病人正在看病!"); } } }
|
上机练习7
需求说明
“桃跑跑” “张票票” “黄牛党” 共同抢十张票,限制黄牛党只能抢一次票,请使用多线程模拟此过程。运行如图所示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.lx7;
public class Test implements Runnable{
private int count = 10; private int num;
@Override public void run() { while (true) { synchronized (this) { if (count == 0) { return; } if ("黄牛党".equals(Thread.currentThread().getName())) { if (num == 1) { break; } } count--; num++; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ",抢到了第" + count + "张票," + "剩余:" + num + "张票"); }
} }
public static void main(String[] args) { Test test = new Test(); Thread t1 = new Thread(test,"桃跑跑"); Thread t2 = new Thread(test,"张票票"); Thread t3 = new Thread(test,"黄牛党");
t1.start(); t2.start(); t3.start(); } }
|
简答题4
编写一个程序,创建两个线程对象,每个线程输出1到5个数。要求线程类分别使用继承Thread类和实现Runnable接口两种方式创建。
使用Runnable接口实现
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.jd5;
public class MyRunnable implements Runnable {
@Override public void run() { System.out.println("使用Runnable接口方式创建线程------"); for (int i = 0; i < 5; i++) { System.out.println("当前线程名是" + Thread.currentThread().getName() + "\t" + (i + 1) + "\t"); } } }
|
使用Thread继承实现
1 2 3 4 5 6 7 8 9 10 11 12
| package com.jd5;
public class MyThread extends Thread { @Override public void run() { System.out.println("使用Thread继承方式创建线程------"); for (int i = 0; i < 5; i++) { System.out.println("当前线程名是" + Thread.currentThread().getName() + "\t" + (i + 1) + "\t"); } } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.jd5;
public class Test { public static void main(String[] args) { MyThread m = new MyThread(); m.start();
Thread t = new Thread(new MyRunnable()); System.out.println(); t.start(); } }
|
简答题5
张三和他的妻子各拥有一张银行卡和存折,可以对同一个银行账户进行存取款操作,现银行账户中余额为500元,每人各取五次,每次取款100元,在取款过程中存在网络延时。现使用多线程模拟这个过程。运行如下
本题由@栗子团友情赞助:QQ:2663272630
线程类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.jd5;
public class MyThread implements Runnable{
private int money = 500;
@Override public void run() { for (int i = 1; i <= 5; i++) { synchronized (this) { if (money == 0) { System.out.println("余额不足以支付" + Thread.currentThread().getName() + "的取款,余额为:" + money); continue; } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } money -= 100; System.out.println(Thread.currentThread().getName() + "准备取款!"); System.out.println(Thread.currentThread().getName() + "完成取款!"); } }
} }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.jd5;
public class Test { public static void main(String[] args) { MyThread m = new MyThread(); Thread a = new Thread(m,"张三"); Thread b = new Thread(m,"张三的妻子");
a.start(); b.start(); } }
|