java实验总结

来源:互联网 发布:ubuntu登录root 编辑:程序博客网 时间:2024/05/18 12:05
   * 实现方式和继承方式有什么区别呢?
 *         区别:
 *                 继承Thread:线程代码存放在Thread子类run方法中
 *                 实现Runnable:线程代码存放在接口的子类的run方法中
 *
 *         实现方式的好处:避免了单继承的局限性
 *         在定义线程时,建议使用实现方式,当然如果一个类没有继承父类,那么也可以通过继承Thread类来实现多线程
 *
 * 注意:Runnable接口没有抛出异常,那么实现它的类只能是try-catch不能throws
 * Java对多线程的安全问题提供了专业的解决方式就是同步代码块synchronized(对象){需要同步的代码}
 *
 * 同步的前提:
 *         1.有2个及以上的线程
 *         2.多个线程使用用一个锁(对象)
 *     同步的好处:解决了多线程的安全问题

 * 同步的弊端:多个线程需要判断锁,较为消耗资源

    package 多线程;            class Ticket implements Runnable      {      //  private static int tick = 100;          private int tick=100;          Object obj = new Object();//創建一個對象或者自己重新写一个类来创建一个对象下面同步关键字需要用到          @Override          public void run()           {              while(true)              {                  synchronized(obj)      //          synchronized(this)                  {                  if(tick>0){                      try {Thread.sleep(10);} catch (Exception e) {   }                      System.out.println(Thread.currentThread().getName()+"...銷售:"+(tick--)+"号票");      //              tick--;                  }else {                      break;                  }                  }              }          }                }      public class Test       {          public static void main(String[] args)           {              Ticket t = new Ticket();//创建一个实现了Runnable接口的类                            //创建4个多线程对象并传递上面接口对象给其构造方法              Thread t1 = new Thread(t);//创建了一个线程              Thread t2 = new Thread(t);//创建了一个线程              Thread t3 = new Thread(t);//创建了一个线程              Thread t4 = new Thread(t);//创建了一个线程                            //开启线程              t1.start();              t2.start();              t3.start();              t4.start();          }      }  

实验六编写简单的计算器

  1. package Computer;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Color;  
  5. import java.awt.Container;  
  6. import java.awt.Font;  
  7. import java.awt.GridLayout;  
  8. import java.awt.event.ActionEvent;  
  9. import java.awt.event.ActionListener;  
  10. import java.util.Stack;  
  11.   
  12. import javax.swing.JApplet;  
  13. import javax.swing.JButton;  
  14. import javax.swing.JFrame;  
  15. import javax.swing.JPanel;  
  16. import javax.swing.JTextField;  
  17.   
  18. public class Count extends JApplet implements ActionListener  
  19. {  
  20.       
  21.     /** 
  22.      *  
  23.      */  
  24.     private static final long serialVersionUID = 1L;  
  25.     private JTextField textField = new JTextField("请输入");  
  26.     String operator = "";//操作  
  27.     String input = "";//输入的 式子  
  28.     boolean flag =  true;  
  29. //  boolean flag1 = true;  
  30. //  boolean flag2 = true;  
  31.     public void init()//覆写Applet里边的init方法  
  32.     {  
  33.         Container C = getContentPane();  
  34.         JButton b[] = new JButton[16];  
  35.         JPanel panel = new JPanel();  
  36.         C.add(textField, BorderLayout.NORTH);  
  37.         C.add(panel,BorderLayout.CENTER);  
  38.         panel.setLayout(new GridLayout(44,5,5));  
  39.         String name[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//设置 按钮  
  40.         for(int i=0;i<16;i++)//添加按钮  
  41.         {  
  42.             b[i] = new JButton(name[i]);  
  43.             b[i].setBackground(new Color(192,192,192));  
  44.             b[i].setForeground(Color.BLUE);//数字键 设置为 蓝颜色  
  45.             if(i%4==3)  
  46.                 b[i].setForeground(Color.RED);  
  47.             b[i].setFont(new Font("宋体",Font.PLAIN,16));//设置字体格式  
  48.             panel.add(b[i]);  
  49.             b[i].addActionListener(this);  
  50.         }  
  51.         b[13].setForeground(Color.RED);//非数字键,即运算键设置为红颜色  
  52.         b[13].setForeground(Color.RED);  
  53.     }  
  54.     public void actionPerformed(ActionEvent e)   
  55.     {  
  56.         int cnt = 0;  
  57.         String actionCommand = e.getActionCommand();  
  58.         if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))  
  59.             input +=" "+actionCommand+" ";//设置输入,把输入的样式改成 需要的样子  
  60.         else if(actionCommand.equals("C"))  
  61.             input = "";  
  62.         else if(actionCommand.equals("="))//当监听到等号时,则处理 input  
  63.         {  
  64.             input+= "="+compute(input);  
  65.             textField.setText(input);  
  66.             input="";  
  67.             cnt = 1;  
  68.         }  
  69.         else  
  70.             input += actionCommand;//数字为了避免多位数的输入 不需要加空格  
  71.         if(cnt==0)  
  72.         textField.setText(input);  
  73.     }  
  74.     private String compute(String input)//即1237 的 样例  
  75.     {  
  76.         String str[];  
  77.         str = input.split(" ");  
  78.         Stack<Double> s = new Stack<Double>();  
  79.         double m = Double.parseDouble(str[0]);  
  80.         s.push(m);  
  81.         for(int i=1;i<str.length;i++)  
  82.         {  
  83.             if(i%2==1)    
  84.             {    
  85.                 if(str[i].compareTo("+")==0)    
  86.                 {    
  87.                     double help = Double.parseDouble(str[i+1]);    
  88.                     s.push(help);    
  89.                 }    
  90.                     
  91.                 if(str[i].compareTo("-")==0)    
  92.                 {    
  93.                     double help = Double.parseDouble(str[i+1]);    
  94.                     s.push(-help);    
  95.                 }    
  96.                     
  97.                 if(str[i].compareTo("*")==0)    
  98.                 {    
  99.                     double help = Double.parseDouble(str[i+1]);    
  100.                     double ans = s.peek();//取出栈顶元素    
  101.                     s.pop();//消栈    
  102.                     ans*=help;    
  103.                     s.push(ans);    
  104.                 }    
  105.                     
  106.                 if(str[i].compareTo("/")==0)    
  107.                 {    
  108.                     double help = Double.parseDouble(str[i+1]);    
  109.                     double ans = s.peek();    
  110.                     s.pop();    
  111.                     ans/=help;    
  112.                     s.push(ans);    
  113.                 }    
  114.             }    
  115.         }    
  116.         double ans = 0d;    
  117.         while(!s.isEmpty())    
  118.         {    
  119.             ans+=s.peek();    
  120.             s.pop();    
  121.         }    
  122.         String result = String.valueOf(ans);  
  123.         return result;  
  124.     }  
  125.     public static void main(String args[])  
  126.     {  
  127.         JFrame frame = new JFrame("Count");  
  128.         Count applet = new Count();  
  129.         frame.getContentPane().add(applet, BorderLayout.CENTER);  
  130.         applet.init();//applet的init方法  
  131.         applet.start();//线程开始  
  132.         frame.setSize(350400);//设置窗口大小  
  133.         frame.setVisible(true);//设置窗口可见  
  134.     }  
  135.   


原创粉丝点击