实验十六 线程技术

                                                                                实验十六  线程技术

                                                                                                  实验时间 2017-12-8

1、实验目的与要求

(1) 掌握线程概念;

(2) 掌握线程创建的两种技术;

(3) 理解和掌握线程的优先级属性及调度方法;

(4) 掌握线程同步的概念及实现技术;

2、实验内容和步骤

实验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();

     }

}

 测试结果:

利用Runnable接口改造程序,掌握用Runnable接口创建线程的方法。源代码如下

package Demo1;

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 left1 = new Lefthand();
        Runnable right1 = new Righthand() ;
        Thread left = new Thread(left1);
        Thread right = new Thread(right1);
        left.start();
        right.start();
    }
}

 

测试程序2

l 在Elipse环境下调试教材625页程序14-1、14-2 14-3,结合程序运行结果理解程序;

l 在Elipse环境下调试教材631页程序14-4,结合程序运行结果理解程序;

l 对比两个程序,理解线程的概念和用途;

l 掌握线程创建的两种技术。

程序运行结果:

运行程序14-4

 

 

测试程序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-5、14-6,结合程序运行结果理解程序;

 

综合编程练习

编程练习1

  1. 设计一个用户信息采集程序,要求如下:

(1) 用户信息输入界面如下图所示:

 

 

(2) 用户点击提交按钮时,用户输入信息显示控制台界面;

(3) 用户点击重置按钮后,清空用户已输入信息;

(4) 点击窗口关闭,程序退出。

2.创建两个线程,每个线程按顺序输出5次“你好”,每个“你好”要标明来自哪个线程及其顺序号。

3. 完善实验十五 GUI综合编程练习程序。

1.实验源代码:

Main:

 

import java.awt.EventQueue;

import javax.swing.JFrame;

public class Mian {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            demo page = new demo();
        });
    }
}

 

WinCenter:

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;

public class WinCenter {
    public static void center(Window win){
        Toolkit tkit = Toolkit.getDefaultToolkit();
        Dimension sSize = tkit.getScreenSize();
        Dimension wSize = win.getSize();
        if(wSize.height > sSize.height){
            wSize.height = sSize.height;
        }
        if(wSize.width > sSize.width) {
            wSize.width = sSize.width;
        }
        win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);
    }
}

demo:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class demo extends JFrame {
    public demo() {
        JPanel panel1 = new JPanel();
        panel1.setPreferredSize(new Dimension(700, 45));
        panel1.setLayout(new GridLayout(1, 4));
        JLabel label1 = new JLabel("Name:");
        JTextField j1 = new JTextField("");
        JLabel label2 = new JLabel("Qualification:");
        JComboBox<Object> j2 = new JComboBox<>();
        j2.addItem("Graduate");
        j2.addItem("Not Graduate");
        panel1.add(label1);
        panel1.add(j1);
        panel1.add(label2);
        panel1.add(j2);

        JPanel panel2 = new JPanel();
        panel2.setPreferredSize(new Dimension(700, 50));
        panel2.setLayout(new GridLayout(1, 4));
        JLabel label3 = new JLabel("Address:");
        JTextArea j3 = new JTextArea();
        JLabel label4 = new JLabel("Hobby:");
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(3, 1));
        p.setBorder(BorderFactory.createLineBorder(null));
        JCheckBox c1 = new JCheckBox("Reading");
        JCheckBox c2 = new JCheckBox("Singing");
        JCheckBox c3 = new JCheckBox("Dancing");
        p.add(c1);
        p.add(c2);
        p.add(c3);
        panel2.add(label3);
        panel2.add(j3);
        panel2.add(label4);
        panel2.add(p);

        JPanel panel3 = new JPanel();
        panel3.setPreferredSize(new Dimension(700, 150));
        FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 20, 40);
        panel3.setLayout(flowLayout1);
        JLabel label5 = new JLabel("Sex:");
        JPanel p1 = new JPanel();
        p1.setLayout(new GridLayout(2,1));
        p1.setBorder(BorderFactory.createLineBorder(null));
        ButtonGroup bu = new ButtonGroup();
        JRadioButton jr1 = new JRadioButton("Male");
        JRadioButton jr2 = new JRadioButton("Female");
        bu.add(jr1);
        bu.add(jr2);
        p1.add(jr1);
        p1.add(jr2);
        panel3.add(label5);
        panel3.add(p1);
        add(panel1);
        add(panel2);
        add(panel3);

        JPanel panel4 = new JPanel();
        panel4.setPreferredSize(new Dimension(700, 150));
        JButton b1 = new JButton("Validate");
        panel4.add(b1);
        JButton b2 = new JButton("Reset");
        panel4.add(b2);
        add(panel4);

        FlowLayout flowLayout = new FlowLayout();
        this.setLayout(flowLayout);
        this.setTitle("Students Detail");
        this.setBounds(200, 200, 800, 400);
        this.setVisible(true);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        b1.addActionListener(new ActionListener() {

        
            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                String xueli = j2.getSelectedItem().toString();
                System.out.println("Name:" + j1.getText());
                System.out.println("Qualification:" + xueli);
                String hobbystring = "Hobby:";
                if (c1.isSelected()) {
                    hobbystring += "Reading";
                }
                if (c2.isSelected()) {
                    hobbystring += "Singing";
                }
                if (c3.isSelected()) {
                    hobbystring += "Dancing";
                }
                System.out.println("Address:" + j3.getText());
                if (jr1.isSelected()) {
                    System.out.println("Sex:Male");
                }
                if (jr2.isSelected()) {
                    System.out.println("Sex:Female");
                }
                System.out.println(hobbystring);
            }
        });
        b2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                // TODO 自动生成的方法存根
                j1.setText(null);
                j3.setText(null);
                j2.setSelectedIndex(0);
                c1.setSelected(false);
                c2.setSelected(false);
                c3.setSelected(false);
                bu.clearSelection();
            }
        });
    }

    public static void main(String args[]) {
        new demo();
    }

}

实验运行结果:

 

 

2.实验源代码

class Lefthand extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(i+":1.你好!");
            try {
                sleep(300);
            } catch (InterruptedException e) {
                System.out.println("Lefthand error.");
            }
        }
    }
}

class Righthand extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println(i+":2.你好!");
            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();
    }
}

实验运行结果:

 实验总结:

        线程与进程的概念:

程序是一段静态的代码,它是应用程序执行的蓝本。

‐进程是程序的一次动态执行,它对应了从代码加载、执行至执行完毕的一个完整过程。

‐操作系统为每个进程分配一段独立的内存空间和系统资源,包括:代码数据以及堆栈等资源。每一个进程的内部数据和状态都是完全独立的。

‐多任务操作系统中,进程切换对CPU资源消耗较大

多线程的概念:

多线程是进程执行过程中产生的多条执行线索。

‐线程是比进程执行更小的单位。

‐线程不能独立存在,必须存在于进程中,同一进程的各线程间共享进程空间的数据。

‐每个线程有它自身的产生、存在和消亡的过程,是一个动态的概念。

‐多线程意味着一个程序的多行语句可以看上去几乎在同一时间内同时运行。

‐线程创建、销毁和切换的负荷远小于进程,又称为轻量级进程(lightweight process)。

中断线程:

 当线程的run方法执行方法体中最后一条语句后,或者出现了在run方法中没有捕获的异常时,线程将终止,让出CPU使用权。

 调用interrupt()方法也可终止线程。void interrupt()

– 向一个线程发送一个中断请求,同时把这个线程的“interrupted”状态置为true。

– 若该线程处于 blocked 状 态 , 会抛出InterruptedException。 

线程阻塞的三种情况:

 等待阻塞 -- 通过调用线程的wait()方法,让线程等待某工作的完成。

 同步阻塞 -- 线程在获取synchronized同步锁失败(因为锁被其它线程所占用),它会进入同步阻塞状态。

 其他阻塞 -- 通过调用线程的sleep()或join()或发出了I/O请求时,线程会进入到阻塞状态。当sleep()状态超时、join()等待线程终止或者超时、或者I/O处理完毕时,线程重新转入就绪状态

线程的状态:

多线程调度:

Java提供一个线程调度器来监控程序启动后进入可运行状态的所有线程。线程调度器按照线程的优先级决定应调度哪些线程来执行。

– 处于可运行状态的线程首先进入就绪队列排队等候处理器资源,同一时刻在就绪队列中的线程可候多个。Java的多线程系统会给每个线程自动分配一个线程的优先级。

守护线程:

 守护线程的惟一用途是为其他线程提供服务。例如计时线程。

 若JVM的运行任务只剩下守护线程时,JVM就退出了。

 在一个线程启动之前,调用setDaemon方法可将线程转换为守护线程(daemon thread)。

实验总结:
       通过本周理论知识的学习以及在实验课上所做的实验,我掌握了线程的概念,并且在实际的编程环境下也懂得了如何去理论线程的知识点在页面布局管理器上编辑一些东西,但是在县城的中断这一方面掌握的还不是太好。
posted @ 2018-12-15 18:47  只当梦醒  阅读(238)  评论(0编辑  收藏  举报