一、判断题(T为正确,F为错误),每题1分
1.如果线程死亡,它便不能运行。(T)
2.在Java中,高优先级的可运行线程会抢占低优先级线程。(F )
3.线程可以用yield方法使低优先级的线程运行。(Y)
4…程序开发者必须创建一个线程去管理内存的分配。(T)
5.一个线程在调用它的start方法,之前,该线程将一直处于出生期。( F)
6.当调用一个正在进行线程的stop( )方法时,该线程便会进入休眠状态。(F)
7.一个线程可以调用yield方法使其他线程有机会运行。(T)
8. 多线程没有安全问题(F)
9. 多线程安全问题的解决方案可以使用Lock提供的具体的锁对象操作(T)
10. Stop()方法是终止当前线程的一种状态(F)
二、选择题(不定项选择题),每题2分
1.Java语言中提供了一个D▁线程,自动回收动态分配的内存。
A.异步
B.消费者
C.守护
D.垃圾收集
2.Java语言避免了大多数的C▁错误。
A.数组下标越界
B.算术溢出
C.内存泄露
D.非法的方法参数
3.有三种原因可以导致线程不能运行,它们是ABD▁。
A.等待
B.阻塞
C.休眠
D.挂起及由于I/O操作而阻塞
4.当▁C方法终止时,能使线程进入死亡状态。
A.run
B.setPrority//更改线程优先级
C.yield//暂停当前线程的执行 执行其他线程
D.sleep//线程休眠
5.用▁B方法可以改变线程的优先级。
A.run
B.setPrority
C.yield
D.sleep
6.线程通过▁C▁方法可以使具有相同优先级线程获得处理器。
A.run
B.setPrority
C.yield
D.sleep
7.线程通过▁D▁方法可以休眠一段时间,然后恢复运行。
A.run
B.setPrority
C.yield
D.sleep
8.方法resume( )负责重新开始▁D▁线程的执行。
A.被stop( )方法停止
B.被sleep( )方法停止
C.被wait( )方法停止
D.被suspend( )方法停止
9.▁C▁方法可以用来暂时停止当前线程的运行。
A.stop( )
B.sleep( )
C.wait( )
D.suspend( )
10. 请问下列哪些类是定义在java.io包中的抽象类(ABCDEF)
A. InputStream
B. OutputStream
C. PrintStream
D. Reader
E. FileInputStream
F. FileWriter
三、简述题,每题5分
1.简述程序、进程和线程之间的关系?什么是多线程程序?
程序是应用层的表述,一个程序的运行可以包含多个进程(多进程是为了提高CPU的利用率),一个进程可以有多个线程(多线程是为了提高程序的执行效率)
3.什么是线程调度?Java的线程调度采用什么策略?
线程的调度即时线程之间的通信,一个线程影响另一个线程的运行,(生产者消费者模式)
4.如何在Java程序中实现多线程?
方式一:继承自Thread
开发步骤: 自定义一个类继承者
重写run方法
创建自定义类对象
调用start方法
方式二:实现Runnable接口
创建自定义类实现Runnable接口
创建自定义类对象
将该对象作为形参传递(Thread的有参构造)
启动线程
5.试简述Thread类的子类或实现Runnable接口两种方法的异同?
相同点:
启动线程:调用start方法
不同带你:
Thread继承的方式,该类是多线程类
Runnable接口实现的方式,最后使用Thread的有参构造
6.说明缓冲流的优点和原理
提高执行效率
创建一个缓冲区内存,一次一个操作多个,保存在该缓冲区内存,提高效率
8:在Java中wait()和sleep()方法的不同?
wait()方法的调用会释放锁对象
sleep()方法的调用不会释放锁对象
9:Java中Runnable和Callable有什么不同?
Runnable是重写run()方法,配合Thread的有参构造使用
Callable是重写call()方法,配合submit()使用
四、程序设计题
1.编写一个应用程序,在线程同步的情况下来实现“生产者―消费者”问题。
package weekends_11;
public class Thread_produce {
public static void main(String[] args) {
Product p=new Product();
SetDemo s=new SetDemo(p);
GetDemo g=new GetDemo(p);
s.start();
g.start();
}
}
class Product {
String name;
}
class SetDemo extends Thread {
Product p;
public SetDemo(Product p) {
this.p = p;
}
@Override
public void run() {
p.name="Tubo";
}
}
class GetDemo extends Thread {
Product p;
public GetDemo(Product p) {
this.p = p;
}
@Override
public void run() {
System.out.println(p.name);
}
}
运行结果:
Tubo
2.修改上题,由于各个线程是异步运行的,因此无法预计其相对速度,为了使生产者能够不断地生产,可以使用循环缓冲区,保证有足够多的内存区保存更多的产品。(生产者——仓库——消费者)
package weekends_11;
public class SetStudent extends Thread{
Student s;
private int i=0;
public SetStudent(Student s) {
this.s=s;}
@Override
public void run() {
this.s=s;
while(true) {
try {
this.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i++;
s.set("Tubo"+i);
}
}
}
package weekends_11;
public class GetStudent extends Thread{
Student s;
public GetStudent(Student s) {
this.s=s;
}
@Override
public void run() {
while(true)
{try {
this.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s.get();
}
}
}package weekends_11;
import java.io.Serializable;
import java.util.ArrayList;
public class Student implements Serializable {
@Override
public String toString() {
return "Student [name=" + name + ", i=" + i + "]";
}
String name;
int i=0;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name) {
super();
this.name = name;
}
static ArrayList<Student> al = new ArrayList<Student>();
public synchronized void get() {
if (al.isEmpty()) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("出错了");
}
}
Student student = al.remove(0);
System.out.println(student.name);
this.notify();
}
public synchronized void set(String name) {
if (!al.isEmpty()) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("出错了");
}
}
this.name = name;
al.add(this);
this.notify();
}
}
package weekends_11;
public class StudentDemo {
public static void main(String[] args) {
Student s = new Student();
GetStudent g=new GetStudent(s);
SetStudent s1=new SetStudent(s);
g.start();
s1.start();
}
}
运行结果:
Tubo1
Tubo2
Tubo3
Tubo4
...(每隔两秒钟打印一个)
3 :
1)将若干个Student对象;若干个Teacher对象,写出到d:/0404/a.txt中,
2)将该文件中所有的Student对象反序列化回来,装入List,所有的Teacher对象反序列化回来装入另一个List
package weekends_11;
import java.util.ArrayList;
import java.util.List;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class OutputStreamDemo {
public static void main(String[] args) throws IOException, Exception{
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("E:/0404/a.txt"));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("E:/0404/a.txt"));
List<Student> StudentList=new ArrayList<Student>();
List<Teacher> TeacherList=new ArrayList<Teacher>();
Student s1=new Student("Tubo1");
Student s2=new Student("Tubo2");
Student s3=new Student("Tubo3");
Teacher t1=new Teacher("Tubo4");
Teacher t2=new Teacher("Tubo5");
Teacher t3=new Teacher("Tubo6");
oos.writeObject(t1);
oos.writeObject(t2);
oos.writeObject(t3);
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
for(int i=0;i<6;i++) {
Object rb = ois.readObject();
if(rb.getClass().toString().endsWith("Teacher")) {
TeacherList.add( (Teacher) rb);
continue;
}StudentList.add( (Student) rb);
}
for(Student s:StudentList) {
System.out.println(s.name);
}
for(Teacher s:TeacherList) {
System.out.println(s.name);
}
}
}
运行结果:
Tubo1
Tubo2
Tubo3
Tubo4
Tubo5
Tubo6
4:实现字符串和字节数组之间的相互转换,比如:将字符串”西部开源技术中心xbkyjszx”转换为字节数组,并将字节数组再转换回字符串!
package weekends_11;
import java.util.Scanner;
public class StringDemo {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("请随意输入一串字符");
String newStr = sc.nextLine();
byte[] charArray = newStr.getBytes();
for(byte s:charArray) {
System.out.println(s);
}
String Str = new String(charArray);
System.out.println(Str);
}
}
请随意输入一串字符
Java 我来了
74
97
118
97
32
-50
-46
-64
-76
-63
-53
Java 我来了
5:用Java编程一个会导致死锁的程序,你将怎么解决?请你设计
使用生产者消费者的模式,一个线程唤醒另一个线程
package weekends;
public class DieLock {
Boolean flag = true;
String name = "Tubo";
public static void main(String[] args) {
DieLock d = new DieLock();
Produce p = new Produce(d);
Consum c = new Consum(d);
p.start();
c.start();
}
}
class Produce extends Thread {
DieLock d;
public Produce(DieLock d) {
this.d = d;
}
@Override
public synchronized void run() {
while (true) {
if (d.flag == true) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println("出错额");
}
}
d.name = "Tubo";
d.flag = true;
//c.notify();
}
}
}
class Consum extends Thread {
DieLock d;
public Consum(DieLock d) {
this.d = d;
}
@Override
public synchronized void run() {
while (true) {
if (d.flag == false) {
try {
this.wait();
} catch (InterruptedException e) {
System.out.println("出错了");
}
}
System.out.println(d.name);
d.flag = false;
//p.notify();
}
}
}
//程序处于等待状态
6:递归实现输入任意目录,列出文件以及文件夹
package weekends_11;
import java.io.File;
import java.util.Scanner;
public class NewFile {
private static int i=0;
public static void main(String[] args) {
System.out.println("请输入你要查看的盘符");
Scanner sc=new Scanner(System.in);
String line = sc.nextLine();
File f=new File(line);
searchFile(f);
}
private static void searchFile(File f) {
String path = f.getPath();
System.out.println(path);
File[] files = f.listFiles();
if(files!=null) {
for(File file:files) {
if(file.isDirectory()) {
System.out.println(file);
searchFile(file);
continue;
}
System.out.println(file);
}
}
}
}
请输入你要查看的盘符
请输入你要查看的盘符
E:\JavaSE
E:\JavaSE
E:\JavaSE\ArrayPractice.java
E:\JavaSE\digitl.java
E:\JavaSE\Method.java
E:\JavaSE\SortPractice.class