zoukankan      html  css  js  c++  java
  • 李晓菁201771010114《面向对象程序设计Java》第十六周学习总结

    一:理论知识

    1.线程的概念:

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

    多线程是进程执行过程中产生的多条执行线索。
    ‐线程是比进程执行更小的单位。
    ‐线程不能独立存在,必须存在于进程中,同一进
    程的各线程间共享进程空间的数据。
    ‐每个线程有它自身的产生、存在和消亡的过程,
    是一个动态的概念。
    ‐多线程意味着一个程序的多行语句可以看上去几
    乎在同一时间内同时运行。

    (2)Java中实现多线程的途径有两种:

    ‐创建Thread类的子类

    <1>用Thread类的子类创建线程

    首先需从Thread类派生出一个子类,在该子类中
    重写run()方法。
    例:
    class hand extends Thread
    {
    public void run()
    {……}
    }

    <2>然后用创建该子类的对象
    Lefthand left=new Lefthand();
    Righthand right=new Righthand();
    <3>最后用start()方法启动线程
    left.start();
    right.start();

    用Thread类的子类创建多线程的关键性操作

    –定义Thread类的子类并实现用户线程操作,即

    run()方法的实现。

    –在适当的时候启动线程。

    由于Java只支持单重继承,用这种方法定义的类不可再继承其他父类。

    ‐在程序中定义实现Runnable接口的类

    (2)用Runnable()接口实现线程:

    ⚫ 首先设计一个实现Runnable接口的类;
    ⚫ 然后在类中根据需要重写run方法;
    ⚫ 再创建该类对象,以此对象为参数建立Thread
    类的对象;
    ⚫ 调用Thread类对象的start方法启动线程,将
    CPU执行权转交到run方法。

    2.中断线程:

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

    ⚫ 调用interrupt()方法也可终止线程。
    void interrupt()
    – 向一个线程发送一个中断请求,同时把这个线
    程的“interrupted”状态置为true。
    – 若该线程处于 blocked 状 态 , 会抛出
    InterruptedException。

    (2)测试线程是否被中断的方法

    Java提供了几个用于测试线程是否被中断的方法。
    ⚫ static boolean interrupted()
    – 检测当前线程是否已被中断 , 并重置状态
    “interrupted”值为false。
    ⚫ boolean isInterrupted()
    – 检测当前线程是否已被中断 , 不改变状态
    “interrupted”值 。

    3.线程状态:

    利用各线程的状态变换,可以控制各个线程轮流
    使用CPU,体现多线程的并行性特征。
    ⚫ 线程有如下7种状态:
    ➢ New (新建)

    ⚫ new(新建)
    线程对象刚刚创建,还没有启动,此时线程
    还处于不可运行状态。例如:
    Thread thread=new Thread(r);
    此时线程thread处于新建状态,有了相应的
    内存空间以及其它资源。

    ➢ Runnable (可运行)

    ⚫ runnable(可运行状态)
    ➢ 此时线程已经启动,处于线程的run()方法之
    中。
    ➢ 此时的线程可能运行,也可能不运行,只要
    CPU一空闲,马上就会运行。
    ➢ 调用线程的start()方法可使线程处于“可运
    行”状态。例如:
    thread.start();

    ➢ Running(运行)

    ➢ Blocked (被阻塞)

    ⚫ blocked (被阻塞)
    ➢ 一个正在执行的线程因特殊原因,被暂停执行,
    进入阻塞状态。
    ➢ 阻塞时线程不能进入队列排队,必须等到引起
    阻塞的原因消除,才可重新进入排队队列。
    ➢ 引起阻塞的原因很多,不同原因要用不同的方
    法解除。
    ⚫ sleep(),wait()是两个常用引起线程阻塞的方法。

    ➢ Waiting (等待)

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

    ➢ Timed waiting (计时等待)
    ➢ Terminated (被终止)

    ⚫ Terminated (被终止) 线程被终止的原因有二:
    ➢ 一是run()方法中最后一个语句执行完毕而自
    然死亡。
    ➢ 二是因为一个没有捕获的异常终止了run方法
    而意外死亡。
    ➢ 可以调用线程的 stop 方 法 杀 死 一 个 线 程
    (thread.stop();),但是,stop方法已过时,
    不要在自己的代码中调用它。

    其他判断和影响线程状态的方法

    ➢join():等待指定线程的终止。

    ➢join(long millis):经过指定时间等待终止指定
    的线程。
    ➢isAlive():测试当前线程是否在活动。
    ➢yield():让当前线程由“运行状态”进入到“就
    绪状态”从而让其它具有相同优先级的等待线程
    获取执行权。

    4.多线程调度:

    – Java提供一个线程调度器来监控程序启动后进入
    可运行状态的所有线程。线程调度器按照线程的
    优先级决定应调度哪些线程来执行。
    – 处于可运行状态的线程首先进入就绪队列排队等
    候处理器资源,同一时刻在就绪队列中的线程可
    能有多个。Java的多线程系统会给每个线程自动
    分配一个线程的优先级。

    ⚫ Java 的线程调度采用优先级策略:
    ➢ 优先级高的先执行,优先级低的后执行;
    ➢ 多线程系统会自动为每个线程分配一个优先级,缺省
    时,继承其父类的优先级;
    ➢ 任务紧急的线程,其优先级较高;
    ➢ 同优先级的线程按“先进先出”的队列原则;

    ⚫ 下面几种情况下,当前运行线程会放弃CPU:
    – 线程调用了yield() 或sleep() 方法;
    – 抢先式系统下,有高优先级的线程参与调度;
    – 由于当前线程进行I/O访问、外存读写、等待用
    户输入等操作导致线程阻塞;或者是为等候一
    个条件变量,以及线程调用wait() 方法。

    多线程并发执行中的问题:

    ◆多个线程相对执行的顺序是不确定的。
    ◆线程执行顺序的不确定性会产生执行结果的不
    确定性。
    ◆在多线程对共享数据操作时常常会产生这种不
    确定性。

    5.线程同步:

    多线程并发运行不确定性问题解决方案:引入线
    程同步机制,使得另一线程要使用该方法,就只
    能等待。

     线程同步该部分将在下周详细讲述。

    二:实验部分:

    实验十六  线程技术

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

         }

    }

    package threadPool;
    
    import javax.print.attribute.standard.RequestingUserName;
    
    class Lefthand extends Thread  { //用thread的扩展类实现创建线程的方法,创建左手线程
           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  extends Thread {//创建右手线程
            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)
             {     left=new Lefthand();
                   right=new Righthand();
                   left.start();//使该线程开始执行;Java 虚拟机调用该线程的 run 方法。 
                   right.start();//结果是两个线程并发地运行;当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)。 
             }
        }
    threadPool

    用Runnable接口改造后的程序

    package threadPool;
    
    import javax.print.attribute.standard.RequestingUserName;
    
    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)
             {   //  left=new Lefthand();
                   //right=new Righthand();
                
                   Runnable lefthand=new Lefthand();
                   Thread left=new Thread(lefthand);// Runnable没有start这个方法,需要将Runnable的对象被Thread引用才可在Thread中调用start方法
                    
                
                   Runnable righthand=new Righthand();
                   Thread right=new Thread(righthand);
                   left.start();
                   right.start();
             }
        }
    threadpool

    测试程序2

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

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

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

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

    程序的前两个部分相同,在14-4中不再赘述

    package bounceThread;
    
    import java.awt.geom.*;
    
    /**
       A ball that moves and bounces off the edges of a 
       rectangle
     * @version 1.33 2007-05-17
     * @author Cay Horstmann
    */
    public class Ball//该类主要定义了小球的大小及其属性
    {
       private static final int XSIZE = 15;
       private static final int YSIZE = 15;
       private double x = 0;
       private double y = 0;
       private double dx = 1;
       private double dy = 1;
    
       /**
          Moves the ball to the next position, reversing direction
          if it hits one of the edges
       */
       public void move(Rectangle2D bounds)
       {
          x += dx;
          y += dy;
          if (x < bounds.getMinX())
          { 
             x = bounds.getMinX();
             dx = -dx;
          }
          if (x + XSIZE >= bounds.getMaxX())
          {
             x = bounds.getMaxX() - XSIZE; 
             dx = -dx; 
          }
          if (y < bounds.getMinY())
          {
             y = bounds.getMinY(); 
             dy = -dy;
          }
          if (y + YSIZE >= bounds.getMaxY())
          {
             y = bounds.getMaxY() - YSIZE;
             dy = -dy; 
          }
       }
    
       /**
          Gets the shape of the ball at its current position.
       */
       public Ellipse2D getShape()
       {
          return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
       }
    }
    ball
    package bounceThread;
    
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    
    /**
     * The component that draws the balls.
     * @version 1.34 2012-01-26
     * @author Cay Horstmann
     */
    public class BallComponent extends JComponent
    {
       private static final int DEFAULT_WIDTH = 450;
       private static final int DEFAULT_HEIGHT = 350;
    
       private java.util.List<Ball> balls = new ArrayList<>();//构造一个初始容量为 10 的空列表。
    //此接口的用户可以对列表中每个元素的插入位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
       /**
        * Add a ball to the panel.
        * @param b the ball to add
        */
       public void add(Ball b)
       {
          balls.add(b);
       }
    
       public void paintComponent(Graphics g)
       {
          Graphics2D g2 = (Graphics2D) g;
          for (Ball b : balls)
          {
             g2.fill(b.getShape());
          }
       }
       
       public Dimension getPreferredSize() { return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); }
    }
    bounceThreade
    package bounce;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    /**
     * Shows an animated bouncing ball.
     * @version 1.34 2015-06-21
     * @author Cay Horstmann
     */
    public class Bounce
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {
             JFrame frame = new BounceFrame();
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          });
       }
    }
    
    /**
     * The frame with ball component and buttons.
     */
    class BounceFrame extends JFrame
    {
       private BallComponent comp;
       public static final int STEPS = 1000;
       public static final int DELAY = 3;
    
       /**
        * Constructs the frame with the component for showing the bouncing ball and
        * Start and Close buttons
        */
       public BounceFrame()
       {
          setTitle("Bounce");
          comp = new BallComponent();
          add(comp, BorderLayout.CENTER);//将自建的swing组件放在居中的位置
          JPanel buttonPanel = new JPanel();
          addButton(buttonPanel, "Start", event -> addBall());
          addButton(buttonPanel, "Close", event -> System.exit(0));//将两个组件注册为事件源并添加到buttonpanel中
          add(buttonPanel, BorderLayout.SOUTH);//将一个buttonpanel放到框架的南端
          pack();
       }
    
       /**
        * Adds a button to a container.
        * @param c the container
        * @param title the button title
        * @param listener the action listener for the button
        */
       public void addButton(Container c, String title, ActionListener listener)
       {
          JButton button = new JButton(title);
          c.add(button);
          button.addActionListener(listener);
       }//用addbutton方法将两个按钮组合为一个整体
    
       /**
        * Adds a bouncing ball to the panel and makes it bounce 1,000 times.
        */
       public void addBall()
       {
           
         try
          {
             Ball ball = new Ball();
             comp.add(ball);
    
             for (int i = 1; i <= STEPS; i++)
             {
                ball.move(comp.getBounds());
                comp.paint(comp.getGraphics());
                Thread.sleep(DELAY);//调用sleep并未创建线程对象,只是为了将两个球的轨迹可以显示出来
             }
          }
          catch (InterruptedException e)
          {
          }
       }
    }
    bounce

    弥补缺陷后的程序(14-4):

    package bounceThread;
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    /**
     * Shows animated bouncing balls.
     * @version 1.34 2015-06-21
     * @author Cay Horstmann
     */
    public class BounceThread
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {
             JFrame frame = new BounceFrame();
             frame.setTitle("BounceThread");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          });
       }
    }
    
    /**
     * The frame with panel and buttons.
     */
    class BounceFrame extends JFrame
    {
       private BallComponent comp;
       public static final int STEPS = 1000;
       public static final int DELAY = 5;
    
    
       /**
        * Constructs the frame with the component for showing the bouncing ball and
        * Start and Close buttons
        */
       public BounceFrame()
       {
          comp = new BallComponent();
          add(comp, BorderLayout.CENTER);
          JPanel buttonPanel = new JPanel();
          addButton(buttonPanel, "Start", event -> addBall());
          addButton(buttonPanel, "Close", event -> System.exit(0));
          add(buttonPanel, BorderLayout.SOUTH);
          pack();
       }
    
       /**
        * Adds a button to a container.
        * @param c the container
        * @param title the button title
        * @param listener the action listener for the button
        */
       public void addButton(Container c, String title, ActionListener listener)
       {
          JButton button = new JButton(title);
          c.add(button);
          button.addActionListener(listener);
       }
    
       /**
        * Adds a bouncing ball to the canvas and starts a thread to make it bounce
        */
       public void addBall()
       {
          Ball ball = new Ball();
          comp.add(ball);
          Runnable r = () -> { //使用匿名内部类
             try
             {  
                for (int i = 1; i <= STEPS; i++)
                {
                   ball.move(comp.getBounds());
                   comp.repaint();
                   Thread.sleep(DELAY);
                }
             }
             catch (InterruptedException e)
             {
             }
          };
          Thread t = new Thread(r);
          t.start();//使得小球的移动成为其中的一个子线程
       }
    }
    bounceThread

    两个程序的不同之处在于:第一个程序存在一定的缺陷,在该程序中,小球的运动轨迹的显示只是程序的一部分,

    当点击start开始后,在程序运行过程中,即显示小球运动轨迹过程中,并不能通过close或者右上角的关闭界面键来关闭程序;

    第二个程序是对第一个程序的缺陷进行了优化,它将小球的显示轨迹创建为程序的一个子线程,这样在小球显示轨迹过程中,可随时关闭界面。

    测试程序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()+"已计算完毕!");

        }

    }

    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);}//改变1号与3号线程的优先级
      public void run( ) {
         for(int i=0; i<1000000; i++);//执行空循环,在该时间内1号与3号线程优先级的改变就已经起了作用
          
          System.out.println(getName()+"线程的优先级是"+getPriority()+"已计算完毕!");
        }
    }
    race

    测试程序4

    l 教材642页程序模拟一个有若干账户的银行,随机地生成在这些账户之间转移钱款的交易。每一个账户有一个线程。在每一笔交易中,会从线程所服务的账户中随机转移一定数目的钱款到另一个随机账户。

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

    package unsynch;
    
    import java.util.*;
    
    /**
     * A bank with a number of bank accounts.
     * @version 1.30 2004-08-01
     * @author Cay Horstmann
     */
    public class Bank
    {
       private final double[] accounts;
    
       /**
        * Constructs the bank.
        * @param n the number of accounts
        * @param initialBalance the initial balance for each account
        */
       public Bank(int n, double initialBalance)
       {
          accounts = new double[n];
          Arrays.fill(accounts, initialBalance); //该句代码理解有一点困难
       }
    
       /**
        * Transfers money from one account to another.
        * @param from the account to transfer from
        * @param to the account to transfer to
        * @param amount the amount to transfer
        */
       public void transfer(int from, int to, double amount)
       {
          if (accounts[from] < amount) return;
          System.out.print(Thread.currentThread());//返回对当前正在执行的线程对象的引用。
          accounts[from] -= amount;
          System.out.printf(" %10.2f from %d to %d", amount, from, to);
          accounts[to] += amount;
          System.out.printf(" Total Balance: %10.2f%n", getTotalBalance());//
       }
    
       /**
        * Gets the sum of all account balances.
        * @return the total balance
        */
       public double getTotalBalance()
       {
          double sum = 0;
    
          for (double a : accounts)
             sum += a;
    
          return sum;
       }
    
       /**
        * Gets the number of accounts in the bank.
        * @return the number of accounts
        */
       public int size()
       {
          return accounts.length;
       }
    }
    bank
    package unsynch;
    
    /**
     * This program shows data corruption when multiple threads access a data structure.
     * @version 1.31 2015-06-21
     * @author Cay Horstmann
     */
    public class UnsynchBankTest
    {
       public static final int NACCOUNTS = 100;
       public static final double INITIAL_BALANCE = 1000;
       public static final double MAX_AMOUNT = 1000;
       public static final int DELAY = 10;
       
       public static void main(String[] args)
       {
          Bank bank = new Bank(NACCOUNTS, INITIAL_BALANCE);//新创建一个带入口参数的bank对象
          for (int i = 0; i < NACCOUNTS; i++)
          {
             int fromAccount = i;
             Runnable r = () -> {
                try
                {
                   while (true)
                   {
                      int toAccount = (int) (bank.size() * Math.random());
                      double amount = MAX_AMOUNT * Math.random();
                      bank.transfer(fromAccount, toAccount, amount);
                      Thread.sleep((int) (DELAY * Math.random()));//在指定的毫秒数内让当前正在执行的线程休眠(暂停执行).
                   }
                }
                catch (InterruptedException e)//当线程在活动之前或活动期间处于正在等待、休眠或占用状态且该线程被中断时,抛出该异常
                {
                }            
             };
             Thread t = new Thread(r);
             t.start();//使该线程开始执行;Java 虚拟机调用该线程的 run 方法。 
          }
       }
    }
    UnsynchBankTest

    综合编程练习

    编程练习1

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

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

     

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

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

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

    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    
    public class Main {
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                DemoJFrame page = new DemoJFrame();
            });
        }
    }
    main
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.LayoutManager;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.swing.BorderFactory;
    import javax.swing.ButtonGroup;
    import javax.swing.ButtonModel;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    public class DemoJFrame extends JFrame {
        private JPanel jPanel1;
        private JPanel jPanel2;
        private JPanel jPanel3;
        private JPanel jPanel4;
        private JTextField fieldname;
        private JComboBox comboBox;
        private JTextField fieldadress;
        private ButtonGroup bg;
        private JRadioButton male;
        private JRadioButton female;
        private JCheckBox read;
        private JCheckBox movie;
        private JCheckBox learn;
    
        public DemoJFrame() {
            // 设置窗口大小
            this.setSize(800, 400);
            // 设置可见性
            this.setVisible(true);
            // 设置标题
            this.setTitle("编程练习一");
            // 设置关闭操作
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            // 设置窗口居中
            WinCenter.center(this);
            // 创建四个面板对象
            jPanel1 = new JPanel();
            setJPanel1(jPanel1);
            jPanel2 = new JPanel();
            setJPanel2(jPanel2);
            jPanel3 = new JPanel();
            setJPanel3(jPanel3);
            jPanel4 = new JPanel();
            setJPanel4(jPanel4);
            // 设置容器的为流布局
            FlowLayout flowLayout = new FlowLayout();//构造一个新的 FlowLayout,它是居中对齐的
            this.setLayout(flowLayout);
            // 将四个面板添加到容器中
            this.add(jPanel1);
            this.add(jPanel2);
            this.add(jPanel3);
            this.add(jPanel4);
    
        }
    
        /*
         * 设置面一
         */
        private void setJPanel1(JPanel jPanel) {
            // TODO 自动生成的方法存根
            jPanel.setPreferredSize(new Dimension(700, 45));
            // 给面板的布局设置为网格布局 一行4列
            jPanel.setLayout(new GridLayout(1, 4));
            JLabel name = new JLabel("姓名:");
            name.setSize(100, 50);
            fieldname = new JTextField("");
            fieldname.setSize(80, 20);
            JLabel study = new JLabel("学历:");
            comboBox = new JComboBox();
            comboBox.addItem("初中");
            comboBox.addItem("高中");
            comboBox.addItem("本科");
            jPanel.add(name);
            jPanel.add(fieldname);
            jPanel.add(study);
            jPanel.add(comboBox);
    
        }
    
        /*
         * 设置面板二
         */
        private void setJPanel2(JPanel jPanel) {
            // TODO 自动生成的方法存根
            jPanel.setPreferredSize(new Dimension(700, 50));
            // 给面板的布局设置为网格布局 一行4列
            jPanel.setLayout(new GridLayout(1, 4));
            JLabel name = new JLabel("地址:");
            fieldadress = new JTextField();
            fieldadress.setPreferredSize(new Dimension(150, 50));
            JLabel study = new JLabel("爱好:");
            JPanel selectBox = new JPanel();
            selectBox.setBorder(BorderFactory.createTitledBorder(""));//创建一个新标题边框
            selectBox.setLayout(new GridLayout(3, 1));//创建具有指定行数和列数的网格布局。给布局中的所有组件分配相等的大小。
            read = new JCheckBox("读书");
            movie = new JCheckBox("看电影");
            learn = new JCheckBox("学习");
            selectBox.add(read);
            selectBox.add(movie);
            selectBox.add(learn);
            jPanel.add(name);
            jPanel.add(fieldadress);
            jPanel.add(study);
            jPanel.add(selectBox);
        }
    
        /*
         * 设置面板三
         */
        private void setJPanel3(JPanel jPanel) {
            // TODO 自动生成的方法存根
            jPanel.setPreferredSize(new Dimension(700, 150));
            FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
            jPanel.setLayout(flowLayout);
            JLabel sex = new JLabel("性别:");
            JPanel selectBox = new JPanel();
            selectBox.setBorder(BorderFactory.createTitledBorder(""));
            selectBox.setLayout(new GridLayout(2, 1));
            bg = new ButtonGroup();
            male = new JRadioButton("男");
            female = new JRadioButton("女");
            bg.add(male);
            bg.add(female);
            selectBox.add(male);
            selectBox.add(female);
            jPanel.add(sex);
            jPanel.add(selectBox);
    
        }
    
        /*
         * 设置面板四
         */
        private void setJPanel4(JPanel jPanel) {
            // TODO 自动生成的方法存根
            jPanel.setPreferredSize(new Dimension(700, 150));
            FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 50, 10);
            jPanel.setLayout(flowLayout);
            jPanel.setLayout(flowLayout);
            JButton sublite = new JButton("提交");
            JButton reset = new JButton("重置");
            sublite.addActionListener((e) -> valiData());
            reset.addActionListener((e) -> Reset());//给提交和重置按钮创建监听器
            jPanel.add(sublite);
            jPanel.add(reset);
        }
    
        /*
         * 提交数据
         */
        private void valiData() {
            // TODO 自动生成的方法存根
            // 拿到数据
            String name = fieldname.getText().toString().trim();
            String xueli = comboBox.getSelectedItem().toString().trim();
            String address = fieldadress.getText().toString().trim();
            System.out.println(name);
            System.out.println(xueli);
            String hobbystring="";
            if (read.isSelected()) {
                hobbystring+="读书   ";
            }
            if (movie.isSelected()) {
                hobbystring+="看电影   ";
            }
            if (learn.isSelected()) {
                hobbystring+="学习  ";
            }
            System.out.println(address);
            if (male.isSelected()) {
                System.out.println("男");
            }
            if (female.isSelected()) {
                System.out.println("女");
            }
            System.out.println(hobbystring);
        }
    
        /*
         * 重置
         */
        private void Reset() {
            // TODO 自动生成的方法存根
            fieldadress.setText(null);
            fieldname.setText(null);
            comboBox.setSelectedIndex(0);
            read.setSelected(false);
            movie.setSelected(false);
            learn.setSelected(false);
            bg.clearSelection();
        }
    }
    DemoJFrame
    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);
        }
    }
    WinCenter

    点击提交后在控制台的显示:

    点击重置后:

     

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

    package project2;
    
    class Lefthand extends Thread { 
    
           public void run()
    
           {
    
               for(int i=0;i<=4;i++)
    
               {  System.out.println("1.你好");
    
                   try{   sleep(500);   }
    
                   catch(InterruptedException e)
    
                   { System.out.println("Lefthand error.");}    
    
               } 
    
          } 
    
        }
    
        class Righthand extends Thread {
    
            public void run()
    
            {
    
                 for(int i=0;i<=4;i++)
    
                 {   System.out.println("2.你好");
    
                     try{  sleep(300);  }
    
                     catch(InterruptedException e)
    
                     { System.out.println("Righthand error.");}
    
                 }
    
            }
    
        }
    
        public class ThreadText 
    
        {
    
             static Lefthand left;
    
             static Righthand right;
    
             public static void main(String[] args)
    
             {     left=new Lefthand();
    
                   right=new Righthand();
    
                   left.start();
    
                   right.start();
    
             }
    
        }
    ThreadText

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

    实验总结;通过本周的学习我学到了线程的概念,并且掌握了线程创建的两种技术,(1)用Thread类的子类创建线程(2)用Runnable()接口实现线程;

    理解和掌握了线程的优先级属性及调度方法,学到了线程的七种状态。

  • 相关阅读:
    触发器实现从TagBlinkLogs往历史表TagLocationHis20125插入一条数据,实现的是在不同的条件下改变相应的状态
    v$sql,V$SQLTExT和v$sqlarea区别与联系
    centos 下增加swap空间大小
    【转载】telnet: connect to address 127.0.0.1: Connection refused
    【原】centos系统命令部分不可用
    [转]linux下的ssh配置
    [原]linux 配置 ssh 等效性
    ORA03113: endoffile on communication channel Process ID: 252 Session ID: 1 Serial number: 3
    【转载】使用rlwrap增强Linux中的sqlplus命令行功能
    [转]详细解说:简单CSS3实现炫酷读者墙
  • 原文地址:https://www.cnblogs.com/li-xiaojing/p/10116085.html
Copyright © 2011-2022 走看看