201771010125王瑜《面向对象程序设计(java) 》第十六周学习总结html
一 理论知识java
1.程序是一段静态的代码;它是应用程序执行的蓝本。多线程
进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程。学习
2.多线程是进程执行过程当中产生的多条执行线索,县城不能独立存在,必须存在于进程中,同一进程的各线程间共享进程空间的数据测试
3.Java实现多线程有两种途径:spa
(1)建立Thread类的子类:线程
-定义Thread类的子类并实现用户线程操做,即 run()方法的实现;在适当的时候启动线程,因为Java只支持单重继承,用这种方法定义的类不可再继承其余父类。设计
(2)在程序中定义实现Runnable接口的类:3d
-首先设计一个实现Runnable接口的类,而后在类中根据须要重写run方法,再建立该类对象,以此对象为参数创建Thread 类的对象;调用Thread类对象的start方法启动线程,将 CPU执行权转交到run方法调试
4.线程阻塞:
(1)等待阻塞:经过调用线程的wait()方法,让线程等待某工做的完成。
(2)同步阻塞:线程在获取synchronized同步锁失败(由于锁被其它线程所占用),它会进入同步阻塞状态。
(3)其余阻塞:经过调用线程的sleep()或join() 或发出了I/O请求时,线程会进入到阻塞状态。
5.线程的状态
-利用各线程的状态变换,能够控制各个线程轮流使用CPU,体现多线程的并行性特征。
-线程有以下7种状态:
New (新建)
Runnable (可运行)
Running(运行)
Blocked (被阻塞)
Waiting (等待)
Timed waiting (计时等待)
Terminated (被终止)
6.多线程调度
-Java 的线程调度采用优先级策略:
优先级高的先执行,优先级低的后执行;
多线程系统会自动为每一个线程分配一个优先级,缺省时,继承其父类的优先级;
任务紧急的线程,其优先级较高;
同优先级的线程按“先进先出”的队列原则。
二 实验部分
一、实验目的与要求
(1) 掌握线程概念;
(2) 掌握线程建立的两种技术;
(3) 理解和掌握线程的优先级属性及调度方法;
(4) 掌握线程同步的概念及实现技术;
二、实验内容和步骤
实验1:测试程序并进行代码注释。
测试程序1:
l 在elipse IDE中调试运行ThreadTest,结合程序运行结果理解程序;
l 掌握线程概念;
l 掌握用Thread的扩展类实现线程的方法;
l 利用Runnable接口改造程序,掌握用Runnable接口建立线程的方法。
class Lefthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("You are Students!"); try{ sleep(500); } catch(InterruptedException e) { System.out.println("Lefthand error.");} } } } class Righthand extends Thread { public void run() { for(int i=0;i<=5;i++) { System.out.println("I am a Teacher!"); try{ sleep(300); } catch(InterruptedException e) { System.out.println("Righthand error.");} } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left=new Lefthand(); right=new Righthand(); left.start(); right.start(); } }
|
测试结果:
用runable接口实习:
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
|
class
Lefthand
implements
Runnable{
public
void
run()
{
for
(
int
i=
0
;i<=
5
;i++)
{ System.out.println(
"You are Students!"
);
try
{ Thread.sleep(
500
); }
catch
(InterruptedException e)
{ System.out.println(
"Lefthand error."
);}
}
}
}
class
Righthand
implements
Runnable {
public
void
run()
{
for
(
int
i=
0
;i<=
5
;i++)
{ System.out.println(
"I am a Teacher!"
);
try
{ Thread.sleep(
300
); }
catch
(InterruptedException e)
{ System.out.println(
"Righthand error."
);}
}
}
}
public
class
ThreadTest
{
static
Lefthand left;
static
Righthand right;
public
static
void
main(String[] args)
{
Runnable lefthand =
new
Lefthand();
Thread left=
new
Thread(lefthand);
left.start();
Runnable righthand =
new
Righthand();
Thread right=
new
Thread(righthand);
right.start();
}
}
|
测试程序2:
l 在Elipse环境下调试教材625页程序14-一、14-2 、14-3,结合程序运行结果理解程序;
l 在Elipse环境下调试教材631页程序14-4,结合程序运行结果理解程序;
l 对比两个程序,理解线程的概念和用途;
l 掌握线程建立的两种技术。
测试程序3:分析如下程序运行结果并理解程序。
class Race extends Thread { public static void main(String args[]) { Race[] runner=new Race[4]; for(int i=0;i<4;i++) runner[i]=new Race( ); for(int i=0;i<4;i++) runner[i].start( ); runner[1].setPriority(MIN_PRIORITY); runner[3].setPriority(MAX_PRIORITY);} public void run( ) { for(int i=0; i<1000000; i++); System.out.println(getName()+"线程的优先级是"+getPriority()+"已计算完毕!"); } } |
测试程序4
l 教材642页程序模拟一个有若干帐户的银行,随机地生成在这些帐户之间转移钱款的交易。每个帐户有一个线程。在每一笔交易中,会从线程所服务的帐户中随机转移必定数目的钱款到另外一个随机帐户。
l 在Elipse环境下调试教材642页程序14-五、14-6,结合程序运行结果理解程序;
建立两个线程,每一个线程按顺序输出5次“你好”,每一个“你好”要标明来自哪一个线程及其顺序号。
package Project2; class Lefthand extends Thread { public void run() { for(int i=0;i<5;i++) { System.out.println("你好 L"); try{ sleep(500); } catch(InterruptedException e) { System.out.println("Lefthand error.");} } } } class Righthand extends Thread { public void run() { for(int i=0;i<5;i++) { System.out.println("你好 R"); try{ sleep(300); } catch(InterruptedException e) { System.out.println("Righthand error.");} } } } public class ThreadTest { static Lefthand left; static Righthand right; public static void main(String[] args) { left=new Lefthand(); right=new Righthand(); left.start(); right.start(); } } hello
三 实验总结:
经过此次实验,学习了线程的有关知识,理解了实现多线程的两种途径:Thread子类和Runnable接口这两种方法,但在bank的实验中while循环产生随机数以及锁的概念不是很理解。但愿老师能够再讲解一下