柯侧耳倾听者 - 云代码空间
—— 翱翔在Java世界的海洋之上
线程
【实验目的】
1.掌握定制线程的两种方法及多线程间的同步方法。
【实验要求】
1. 编程实现线程的两种定制方法及多线程间的同步方法。
【实验内容】
一、通过继承Thread类定制线程
1.通过继承 Thread 类定制线程;
2.程序功能:实现线程类,打印多行*号;
3.编写test.java程序,代码如下:
package ThreadDemo;
class ThreadDemo extends Thread{
// 重载run函数
public void run(){
for (int count = 1,row = 1; row < 20; row++,count++){
for (int i = 0; i < count; i++){
System.out.print('*');
}
System.out.println();
}
}
}
class test{
public static void main(String argv[]){
ThreadDemo th = new ThreadDemo();
// 调用start()方法执行一个新的线程 th.start();
}
}
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
二、通过实现Runnable接口定制线程
1.通过实现 Runnable 接口定制线程;
2.程序功能:一个时钟Applet,它显示当前时间并逐秒进行更新;
3.编写cc.java程序,代码如下:
package cc;
import java.awt.*;
import java.applet.*;
import java.util.*;
public class cc extends Applet implements Runnable{
Thread clockThread;
public void start(){
if(clockThread==null){
clockThread=new Thread(this,"Clock");
clockThread.start();
}
}
public void run(){
while(clockThread !=null){
repaint();
try{
clockThread.sleep(1000);
}catch(InterruptedException e){}
}
}
public void paint(Graphics g){
Calendar now=Calendar.getInstance();
g.drawString(now.get(Calendar.HOUR)+";"+now.get(Calendar.MINUTE)+";"+now.get(Calendar.SECOND),5,10);
}
public void stop(){
clockThread.stop();
clockThread=null;
}
}
三、多线程同步
1.编写程序实现多线程的同步;
2.程序功能:实现生产者消费者之间的同步;
3.编写cc.java程序,代码如下:
package cc;
class Box{
private int value;
private boolean available=false;
public synchronized int get() throws InterruptedException {
while (available == false) {
}
available = false;
// 通知生产者数据已经被取走,可以再次写入数据
notifyAll();
return value;
}
public synchronized void put(int value) {
while (available == true) {
try {
// 等待消费者取走数据
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.value = value;
available = true;
//通知消费者可以来取数据
notifyAll();
}
}
class Producer extends Thread {
private Box box;
private String name;
public Producer(Box b, String n) {
box = b;
name=n;
}
public void run() {
for (int i = 1; i < 6; i++) {
box.put(i);
System.out.println("Producer " + name
+ " produced: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
class Consumer extends Thread {
private Box box;
private String name;
public Consumer(Box b, String n) {
box=b;
name=n;
}
public void run() {
int value = 0;
for (int i = 1; i < 6; i++) {
value = cc.get();
System.out.println("Consumer " +name
+ " consumed: " + value);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class cc{
public static void main(String[] args) {
Box box = new Box();
Producer p= new Producer(box,"p");
Consumer c= new Consumer(box,"c");
p.start();
c.start();
}
public static int get() {
// TODO Auto-generated method stub
return 0;
}
}
Consumer c consumed: 0
Producer p produced: 1
Consumer c consumed: 0
Consumer c consumed: 0
Consumer c consumed: 0
Consumer c consumed: 0