A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯超 高级黑马   /  2014-5-5 20:46  /  2601 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 冯超 于 2014-5-5 21:15 编辑

question1.
  1. public class First {

  2.         public static void main(String[] args) {
  3.                 // TODO Auto-generated method stub
  4.                 try {
  5.             if() {
  6.                 System.out.print("Hello ");
  7.             }else{
  8.                 System.out.println("world!");
  9.             }
  10.         } catch (Exception e) {
  11.             e.printStackTrace();
  12.         }
  13.         }

  14. }
复制代码
只能在if里面写代码,不能添加类,不能添加jar,不能更改所有内容,只能在if里面写代码 输出 hello world!
        思路:匿名累不累,但是有什么类可以实例化了,匿名类部类一般都是实现的接口或者其他的,可以再里面写一个方法自己实现,object就是!
以下场景中常使用的匿名类部类:
js中:
$("#idName").click(function(e) {});
android中:
Button b = (Button)findViewById("buttonIdName");
b.setOnClickLinstener(new onClickLinsten(){ });
java中:
  1. public class First {

  2.         public static void main(String[] args) {
  3.                 // TODO Auto-generated method stub
  4.                 try {
  5.             if(new Object() {
  6.                     public boolean f() {
  7.                             System.out.println("Hello");
  8.                             return false;
  9.                     }
  10.             }.f()) {
  11.                 System.out.print("Hello ");
  12.             }else{
  13.                 System.out.println("world!");
  14.             }
  15.         } catch (Exception e) {
  16.             e.printStackTrace();
  17.         }
  18.         }

  19. }
复制代码



question2:  如何让自己手机照的图片有倒影的效果呢?

  1. import java.awt.AlphaComposite;
  2. import java.awt.Color;
  3. import java.awt.GradientPaint;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.image.BufferedImage;

  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.SwingUtilities;


  11. @SuppressWarnings("serial")
  12. public class Second extends JPanel {
  13.     private BufferedImage img;

  14.     public Second() {
  15.         img = ImageUtil.loadImage("D://gril2.jpg"); // 源图像文件名
  16.         setBackground(Color.BLACK);
  17.     }

  18.     @Override
  19.     protected void paintComponent(Graphics g) {
  20.         super.paintComponent(g);
  21.         Graphics2D g2d = (Graphics2D) g.create();
  22.         g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  23.             RenderingHints.VALUE_INTERPOLATION_BICUBIC);

  24.         g2d.translate(30, 30);

  25.         int w = img.getWidth();
  26.         int h = img.getHeight();

  27.         // 绘制原图像
  28.         g2d.drawImage(img, 0, 0, null);

  29.         // 绘制反射的图像
  30.         g2d.translate(0, h);
  31.         g2d.drawImage(img, 0, 0, w, (int) (h / 1.5), 0, h, w, 0, null);

  32.         // 绘制透明的渐变
  33.         g2d.setPaint(new GradientPaint(0, 0, new Color(1.0f, 1.0f, 1.0f, 0.2f), 0, h / 2,
  34.             new Color(1.0f, 1.0f, 1.0f, 1.0f)));
  35.         g2d.setComposite(AlphaComposite.DstOut); // 关键就在DstOut
  36.         g2d.fillRect(0, 0, w, h);

  37.         g2d.dispose();
  38.     }

  39.     private static void createGuiAndShow() {
  40.         JFrame frame = new JFrame("Reflected Image");
  41.         frame.getContentPane().add(new Second());

  42.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  43.         frame.setSize(450, 620);
  44.         frame.setLocationRelativeTo(null);
  45.         frame.setAlwaysOnTop(true);
  46.         frame.setVisible(true);
  47.     }

  48.     public static void main(String[] args) {
  49.         SwingUtilities.invokeLater(new Runnable() {
  50.             @Override
  51.             public void run() {
  52.                 createGuiAndShow();
  53.             }
  54.         });
  55.     }
  56. }
复制代码
  1. import java.awt.image.BufferedImage;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;

  5. import javax.imageio.ImageIO;


  6. public class ImageUtil {

  7.         public static BufferedImage loadImage(String imgPath) {
  8.                 // TODO Auto-generated method stub
  9.                 BufferedImage image = null;
  10.                 try {
  11.                         image = ImageIO.read(new FileInputStream(imgPath));
  12.                 } catch (FileNotFoundException e) {
  13.                         // TODO Auto-generated catch block
  14.                         e.printStackTrace();
  15.                 } catch (IOException e) {
  16.                         // TODO Auto-generated catch block
  17.                         e.printStackTrace();
  18.                 }  
  19.                 return image;
  20.         }

  21. }
复制代码
上照片:



gril.jpg (148.51 KB, 下载次数: 33)

倒影图片

倒影图片

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

16 个回复

正序浏览
项目:秒表项目
在具体使用时,如果想计时开始,则按住鼠标,如果想停止计时,则松开鼠标,按住鼠标则计时重新开始,周而复始。

watch类:
  1. package com.feng.watchtool;

  2. import java.awt.AWTEvent;
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Graphics;
  7. import java.awt.event.MouseEvent;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;

  11. public class StopWatch extends Canvas implements Runnable {

  12.         /**
  13.          *
  14.          */
  15.         private Long start;  //开始时间
  16.         private Long end;        //结束时间
  17.         Thread thread_time;  //开始于结束的线程
  18.         boolean flag = true;
  19.         private static final long serialVersionUID = 1L;
  20.         /**
  21.          * @wbp.parser.entryPoint
  22.          */
  23.         public StopWatch() {
  24.                 //this.setBackground(Color.blue);
  25.                 enableEvents(AWTEvent.MOUSE_EVENT_MASK); //注册鼠标事件
  26.                 setBackground(Color.WHITE);
  27.                
  28.                 start  = end = System.currentTimeMillis();
  29.         }
  30.         /**
  31.          * 重新写paint方法  初始化布局
  32.          * @wbp.parser.entryPoint
  33.          */
  34.         public void paint(Graphics g) {
  35.                 // TODO Auto-generated method stub
  36.                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss:SS");
  37.                 Date time = null;
  38.                
  39.                 try {
  40.                         //
  41.                         time = simpleDateFormat.parse("00:00:00");
  42.                 } catch (ParseException e) {
  43.                         // TODO Auto-generated catch block
  44.                         e.printStackTrace();
  45.                 }
  46. //                time.setTime(time.getTime());
  47.                
  48.                 time.setTime(end - start + time.getTime());  //计时
  49.                 String watchTime = simpleDateFormat.format(time);
  50.                 //System.out.println(watchTime);
  51.                
  52.                
  53.                
  54.                
  55.                
  56.                 //化秒表的界面
  57.                 g.drawRect(0, 0, 280, 160);  //画 一个矩形  
  58.                 g.fill3DRect(0, 0, 280, 160, true); //为矩形填充颜色
  59.                 g.setColor(Color.WHITE);  //为字体设置颜色
  60.                 Font font = new Font("Default", Font.PLAIN, 50);  //设置字体的风格
  61.                 g.setFont(font);
  62.                 g.drawString(watchTime, 20, 100);
  63.         }
  64.         //重写鼠标事件 了解事件机制原理
  65.         protected void processMouseEvent(MouseEvent e) {
  66.                 // TODO Auto-generated method stub
  67.                 //鼠标按下
  68.                 if(e.getID() == MouseEvent.MOUSE_PRESSED) {
  69.                         start  = end = System.currentTimeMillis();
  70.                         //重新绘制图
  71.                         repaint();
  72.                        
  73.                         flag = true;
  74.                         new Thread(this).start();  //启动当前线程
  75.                        
  76.                 }
  77.                 //鼠标释放
  78.                 if(e.getID() == MouseEvent.MOUSE_RELEASED) {
  79.                         flag = false;  //结束线程
  80.                         System.out.println(end - start);
  81.                 }
  82.         }
  83.         /**
  84.          *
  85.          */
  86.         public void run() {
  87.                 // TODO Auto-generated method stub
  88.                 while(flag) {
  89.                         end = System.currentTimeMillis();
  90.                         repaint();
  91.                 }
  92.         }
  93.        
  94. }
复制代码

工具类:
  1. package com.feng;

  2. import java.awt.Dimension;
  3. import java.awt.Toolkit;

  4. import javax.swing.JFrame;
  5. import javax.swing.SwingUtilities;

  6. public class SwingConsole {
  7.         public static void run(final JFrame frame, final String title, final int width, final int height) {
  8.                 //一个子线程
  9.                 SwingUtilities.invokeLater(new Runnable() {
  10.                        
  11.                         @Override
  12.                         public void run() {
  13.                                 // TODO Auto-generated method stub
  14.                                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.                                 frame.pack();
  16.                                 frame.setTitle(title);
  17.                                 frame.setSize(width, height);
  18.                                
  19.                                 //显示  并且居中
  20.                                 frame.setLocationRelativeTo(null);                       
  21.                                 frame.setVisible(true);
  22.                                
  23.                         }
  24.                 });
  25.         };
  26. }
复制代码

测试类:
  1. package com.feng.watchtool;

  2. import java.awt.BorderLayout;
  3. import java.awt.Button;
  4. import java.awt.Label;
  5. import java.util.Date;

  6. import javax.swing.JFrame;

  7. import com.feng.SwingConsole;

  8. /**
  9. * 项目需求:秒表项目  模拟现实生活中的秒表
  10. *
  11. * 要求:在具体使用时,如果想计时开始,则按住鼠标,如果想停止计时,则松开鼠标。
  12. * @author fengchao
  13. *
  14. */
  15. public class Watch {

  16.         public static void main(String[] args) {
  17.                 // TODO Auto-generated method stub
  18.                 JFrame frame = new JFrame();
  19.                
  20.                
  21.                
  22.                 StopWatch stopWatch = new StopWatch();
  23.                 Label b1 = new Label("                             鼠标按下 开始计数");
  24.                 Label b2 = new Label("                             鼠标松下 计数停止");
  25.                 Label b3 = new Label("边界");
  26.                 Label b4 = new Label("边界");
  27.                 frame.add(b1, BorderLayout.NORTH);
  28.                 frame.add(b2, BorderLayout.SOUTH);
  29.                 frame.add(b3, BorderLayout.WEST);
  30.                 frame.add(b4, BorderLayout.EAST);
  31.                
  32.                 frame.add(stopWatch, BorderLayout.CENTER);
  33.                 SwingConsole.run(frame, "秒表", 360, 240);
  34.                                
  35. /*                Date date = new Date();
  36.                 System.out.println(date.getTime());
  37.                 System.out.println(System.currentTimeMillis());*/
  38.         }

  39. }
复制代码

事件机制的原理:注册 ----》使用

秒表.png (87.18 KB, 下载次数: 30)

秒表

秒表
回复 使用道具 举报
java中的关机工具原理:
图片如下:
主类:
  1. package com.feng.shutdown;

  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Timer;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;

  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;

  13. public class CloseComputer extends JFrame implements ActionListener {
  14.         private static final long serialVersionUID = 1L;
  15.         private String key;
  16.         private JLabel showTime; //显示倒计时
  17.         private Timer timer; //延时时间
  18.         private boolean time_show = true;   //线程显示时间标示
  19.         private JButton btn_3;
  20.         public CloseComputer() {
  21.                 getContentPane().setLayout(null);
  22.                
  23.                 JLabel label = new JLabel("请选择关机的方式");
  24.                 label.setBounds(34, 9, 128, 15);
  25.                 getContentPane().add(label);
  26.                
  27.                 JButton btn_1 = new JButton("倒计时关机");
  28.                 btn_1.setBounds(10, 112, 120, 23);
  29.                 btn_1.addActionListener(this);
  30.                 getContentPane().add(btn_1);
  31.                
  32.                 JButton btn_2 = new JButton("定时关机");
  33.                 btn_2.setBounds(180, 112, 96, 23);
  34.                 btn_2.addActionListener(this);
  35.                 getContentPane().add(btn_2);
  36.                
  37.                 btn_3 = new JButton("取消关机");
  38.                 btn_3.setEnabled(false);
  39.                 btn_3.setBounds(328, 112, 96, 23);
  40.                 btn_3.addActionListener(this);
  41.                 getContentPane().add(btn_3);
  42.                
  43.                 showTime = new JLabel("");
  44.                 showTime.setBounds(130, 65, 158, 15);
  45.                 getContentPane().add(showTime);
  46.                
  47.         }

  48.         //点击事件
  49.         @Override
  50.         public void actionPerformed(ActionEvent e) {
  51.                 // TODO Auto-generated method stub
  52.                 String ActionCommand = e.getActionCommand();  //获取按钮的名称
  53.                 btn_3.setEnabled(true);
  54.                 if(e.getSource() instanceof JButton) {
  55.                         if("倒计时关机".equals(ActionCommand)) {
  56.                                 timer = new Timer();//类初始化的时候就实例化
  57.                                 time_show = true;
  58.                                 cutDown();
  59.                         }
  60.                         if("定时关机".equals(ActionCommand)) {
  61.                                 timer = new Timer();//类初始化的时候就实例化
  62.                                 timing();
  63.                                
  64.                         }
  65.                         if("取消关机".equals(ActionCommand)) {
  66.                                 System.out.println("取消关机");
  67.                                 btn_3.setEnabled(false);
  68.                                 timer.cancel();  //取消
  69.                                 time_show = false;
  70.                                 //System.out.println(Thread.currentThread().getName());
  71.                         }
  72.                        
  73.                 }
  74.                
  75.         }
  76.         private void cutDown() {
  77.                 // TODO Auto-generated method stub
  78.                 key = JOptionPane.showInputDialog(this, "请输入倒计时关机的时间(m)", "输入框", 1);
  79.                 boolean b = (key != null && isNum(key));
  80.                 if(b) {
  81.                         final Long second = Long.valueOf(key);
  82.                         //如何动态的改变JLabel上面的内容  线程
  83.                     new Thread(new Runnable() {
  84.                                
  85.                                 @Override
  86.                                 public void run() {
  87.                                         // TODO Auto-generated method stub
  88.                                         Long i = second;
  89.                                         while(i > 0 && time_show) {
  90.                                                 try {
  91.                                                         Thread.sleep(1000);
  92.                                                 } catch (InterruptedException e) {
  93.                                                         // TODO Auto-generated catch block
  94.                                                         e.printStackTrace();
  95.                                                 }
  96.                                                 showTime.setText("倒计时" + i + "s关机");
  97.                                                 i--;
  98.                                                 //System.out.println(i);
  99.                                         }
  100.                                 }
  101.                         }).start();;
  102.                         CountTimeTool.delayTime(second, timer);
  103.                 } else {
  104.                         JOptionPane.showMessageDialog(null,"必须输入整形数字");
  105.                 }
  106.         }
  107.         /**
  108.          * 定时工具
  109.          */
  110.         public void timing() {
  111.                 key = JOptionPane.showInputDialog("请输入关机时间(格式xx:xx):");
  112.                 boolean b = (key != null && vaildate(key));
  113.                 if(!b) {
  114.                         JOptionPane.showMessageDialog(this,
  115.                                        "定时时间不对", "系统信息", JOptionPane.ERROR_MESSAGE);
  116.                         return;
  117.                 }
  118.                 String time = getSystemTime();
  119.                
  120.                 long second = getSecond(time, key);
  121.                 if(second <= 0) {
  122.                         JOptionPane.showMessageDialog(this,
  123.                                        "定时时间不对", "系统信息", JOptionPane.ERROR_MESSAGE);
  124.                         return;
  125.                 }
  126.                 showTime.setText("关机时间:" + key);
  127.                 System.out.println(second);
  128.                 CountTimeTool.delayTime(second, timer);
  129.                 //System.out.println(second);
  130.         }
  131.         public boolean isNum(String str) {
  132.                 return str.matches("[0-9]+");
  133.         }
  134.         public static void main(String[] args) {
  135.                 SwingConsole.run(new CloseComputer(), "关机工具", 446, 450);
  136.         }
  137.        
  138.         /**
  139.          *
  140.          * @param date
  141.          * @return
  142.          */
  143.         public  boolean vaildate(String date) {
  144.                 boolean b = false;
  145.                 String reg = "[0-9]{2}:[0-9]{2}";               
  146.                 Pattern p = Pattern.compile(reg);               
  147.                 Matcher m = p.matcher(date);
  148.                 b = m.matches();
  149.                
  150.                 return b;
  151.         }
  152.         /**
  153.          * 获取系统当前时间
  154.          * @return
  155.          */
  156.         public String getSystemTime() {
  157.                 String time = "";
  158.                 Date date = new Date();
  159.         //        System.out.println(date);
  160.                 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
  161.                 time = simpleDateFormat.format(date);
  162.                 //System.out.println(time);
  163.                 return time;
  164.         }
  165.         /**
  166.          * 获取两个时间差 以m为单位
  167.          * @param time1  系统时间
  168.          * @param tim2  输入的时间
  169.          * @return
  170.          */
  171.         public long getSecond(String time1, String time2) {
  172.                 long second = 0l;
  173.                
  174.                 String[] time1_s = time1.split(":");               
  175.                 String[] time2_s = time2.split(":");               
  176.                
  177.                 long hourse = Long.valueOf(time2_s[0]) - Long.valueOf(time1_s[0]);
  178.                 long minute = Long.valueOf(time2_s[1]) - Long.valueOf(time1_s[1]);
  179.                
  180.                 if(hourse < 0) {
  181.                         return 0;
  182.                 }
  183.                
  184.                 second = hourse * 3600 + (minute>0?minute*60:0);
  185.                 //System.out.println(hourse + ":::" + minute);
  186.                
  187.                 return second;
  188.         }
  189. }
复制代码

延时和关机类:
  1. package com.feng.shutdown;

  2. import java.util.Timer;
  3. import java.util.TimerTask;

  4. public class CountTimeTool {

  5.         public static void delayTime(Long second,Timer timer) {
  6.                 // TODO Auto-generated method stub
  7.                 Long delay = 1000l;
  8.                 CutDownTool cut = new CutDownTool();
  9.                 //第一个参数为执行函数
  10.                 timer.schedule(cut, second*delay);
  11.         }

  12. }
复制代码
  1. package com.feng.shutdown;

  2. import java.io.IOException;
  3. import java.util.TimerTask;

  4. public class CutDownTool extends TimerTask {

  5.         @Override
  6.         public void run() {
  7.                 // TODO Auto-generated method stub
  8.                 //System.out.println("执行了关机············");
  9.                
  10.                 try {
  11.                         Runtime.getRuntime().exec("shutdown -s");  //关机程序
  12.                 } catch (IOException e) {
  13.                         // TODO Auto-generated catch block
  14.                         e.printStackTrace();
  15.                 }
  16.         }

  17. }
复制代码

swing工具类:
  1. package com.feng.shutdown;

  2. import javax.swing.JFrame;
  3. import javax.swing.SwingUtilities;

  4. public class SwingConsole {
  5.         public static void run(final JFrame frame, final String title, final int width, final int height) {
  6.                 //一个子线程
  7.                 SwingUtilities.invokeLater(new Runnable() {
  8.                        
  9.                         @Override
  10.                         public void run() {
  11.                                 // TODO Auto-generated method stub
  12.                                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.                                 frame.pack();
  14.                                 frame.setTitle(title);
  15.                                 frame.setSize(width, height);
  16.                                
  17.                                 //显示
  18.                                 frame.setVisible(true);
  19.                                
  20.                         }
  21.                 });
  22.         };
  23. }
复制代码

我把swing程序到出,用bat批处理过,所以安装了jdk jre环境的电脑可以直接双击bat文件可以打开。

代码哪里不懂 可以问群主····

下章:秒表(事件加线程)

shutDown.png (94.93 KB, 下载次数: 34)

shutDown.png

关机工具.rar

157.78 KB, 下载次数: 294

回复 使用道具 举报
question6:
场景:火车站售票系统
* 描述:火车站售票系统用来描述一个售票的过程,如今科技这么复杂,买票的方式
* 也多种多样,而火车站售票系统就是实现   多个人并发购买票的一个过程!


这个也就是设计多个线程共抢能一个资源的过程,同步synch真的是一个很好的解决方法。
tickets:
  1. package com.feng;

  2. public class Tickets implements Runnable {
  3.         private int ticketNumbers;  //该系统共有的票数
  4.         public Tickets(int ticketNumbers) {
  5.                 this.ticketNumbers = ticketNumbers;
  6.         }
  7.         /**
  8.          * 出售票的一个过程
  9.          */       
  10.         public void saleTicket() {
  11.                 while(true) {
  12.                         synchronized (this) {
  13.                                 if(ticketNumbers > 0) {                               
  14.                                         System.out.println("顾客来到了--->" +
  15.                                                         Thread.currentThread().getName()+ "站台");
  16.                                         System.out.println("顾客开始买票");
  17.                                        
  18.                                         try {
  19.                                                 Thread.sleep(5000);
  20.                                         } catch (InterruptedException e) {
  21.                                                 // TODO Auto-generated catch block
  22.                                                 e.printStackTrace();
  23.                                         }
  24.                                         System.out.println("顾客买到了票");
  25.                                         ticketNumbers--;
  26.                                         System.out.println("顾客离开了--->" + Thread.currentThread().getName() + "站台");
  27.                                         System.out.println("总票数为:" + ticketNumbers);
  28.                                 }
  29.                                 if(ticketNumbers == 0 ) {
  30.                                         System.out.println("对不起 票已经卖完········");
  31.                                         break;
  32.                                 }
  33.                         }
  34.                 }
  35.         }
  36.         //
  37.         @Override
  38.         public void run() {
  39.                 // TODO Auto-generated method stub
  40.                 saleTicket();
  41.         }

  42. }
复制代码

测试:
  1. package com.feng;
  2. /**
  3. *
  4. * @author Administrator
  5. * 场景:火车站售票系统
  6. * 描述:火车站售票系统用来描述一个售票的过程,如今科技这么复杂,买票的方式
  7. * 也多种多样,而火车站售票系统就是实现   多个人并发购买票的一个过程!
  8. *     
  9. */
  10. public class Station {

  11.         public static void main(String[] args) {
  12.                 // TODO Auto-generated method stub
  13.                 Tickets ticket = new Tickets(10);
  14.                 Thread th1 = new Thread(ticket);
  15.                 th1.setName("站台1");
  16.                
  17.                 Thread th2 = new Thread(ticket);
  18.                 th2.setName("站台2");
  19.                
  20.                 Thread th3 = new Thread(ticket);
  21.                 th3.setName("站台3");
  22.                
  23.                 th1.start();
  24.                 th2.start();
  25.                 th3.start();
  26.         }

  27. }
复制代码

结果:
顾客来到了--->站台1站台
顾客开始买票
顾客买到了票
顾客离开了--->站台1站台
总票数为:9
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:8
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:7
顾客来到了--->站台3站台
顾客开始买票
顾客买到了票
顾客离开了--->站台3站台
总票数为:6
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:5
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:4
顾客来到了--->站台1站台
顾客开始买票
顾客买到了票
顾客离开了--->站台1站台
总票数为:3
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:2
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:1
顾客来到了--->站台2站台
顾客开始买票
顾客买到了票
顾客离开了--->站台2站台
总票数为:0
对不起 票已经卖完········
对不起 票已经卖完········
对不起 票已经卖完········


下章:java中的关机工具原理,敬请期待
回复 使用道具 举报
ς高眼光の目标 发表于 2014-5-9 05:38
lz 如果有技术分奖励或者会带动更多人猜你这些代码

思想很重要
    自学java 到ssh 从js 到easyui,有的时候代码是敲出来了,但是不知道为什么这么敲。这当做我修炼的一个回忆罢了!
回复 使用道具 举报
lz 如果有技术分奖励或者会带动更多人猜你这些代码
回复 使用道具 举报
你明儿累不累,哈哈,玩笑了!
回复 使用道具 举报
有妹纸,{:3_51:},求介绍
回复 使用道具 举报
楼主威武……
回复 使用道具 举报
楼主对多线程很有研究呀~
回复 使用道具 举报
question5:
场景如下:
     妈妈在下午5点开始到厨房做晚餐,可是在准备做饭的材料时发现
     酱油用完了,因此叫儿子去买。
     妈妈在儿子把酱油买回来之前没什么能干的只好等待。
     直到儿子把酱油买回来,妈妈才开始煮饭,最后终于把饭煮好。

面向对象的思想:
妈妈一个实体
儿子一个实体
    他们各自都是互不干涉线程。

当妈妈正在做饭的过程中,突然让儿子去买酱油,如何让儿子做完事后,妈妈继续完成他的任务呢?
方法是join:
join的理解:
         *    当一个子线程中含有其他子线程的时候,若thread.join
         *    则合并了2个线程,必须按队列输出!


妈妈类:
  1. package com.feng;

  2. public class Mother implements Runnable {
  3.         /**
  4.          * join的理解:
  5.          *    当一个子线程中含有其他子线程的时候,若thread.join
  6.          *    则合并了2个线程,必须按队列输出!
  7.          */
  8.         public void cooking() {
  9.                 System.out.println("妈妈正在做饭。。。");
  10.                 System.out.println("妈妈发现没酱油了。。。");
  11.                 System.out.println("妈妈叫儿子去买啊酱油");
  12.                
  13.                 Son son = new Son();
  14.                 son.start();
  15.                 try {
  16.                         son.join();
  17.                 } catch (InterruptedException e) {
  18.                         // TODO Auto-generated catch block
  19.                         e.printStackTrace();
  20.                 }
  21.                
  22.                
  23.                 System.out.println("妈妈继续做饭");
  24.                 System.out.println("妈妈终于把饭做好了!!");
  25.         }
  26.         @Override
  27.         public void run() {
  28.                 // TODO Auto-generated method stub
  29.                 cooking();
  30.         }
  31. }
复制代码

儿子类:
  1. package com.feng;

  2. public class Son extends Thread {

  3.         @Override
  4.         public void run() {
  5.                 // TODO Auto-generated method stub
  6.                 super.run();
  7.                 buySomething();
  8.         }
  9.         //儿子买酱油的过程
  10.         private void buySomething() {
  11.                 // TODO Auto-generated method stub
  12.                 System.out.println("儿子跑到了商店");
  13.                 System.out.println("儿子买到了酱油");
  14.                 try {
  15.                         Thread.sleep(5000);  //延时5s
  16.                 } catch (InterruptedException e) {
  17.                         // TODO Auto-generated catch block
  18.                         e.printStackTrace();
  19.                 }
  20.                 System.out.println("儿子把酱油交给了妈妈。。。。");
  21.         }

  22. }
复制代码

测试类:
  1. package com.feng;

  2. /**
  3. *
  4. * @author Administrator
  5. * 妈妈在下午5点开始到厨房做晚餐,可是在准备做饭的材料时发现
  6. * 酱油用完了,因此叫儿子去买。
  7. * 妈妈在儿子把酱油买回来之前没什么能干的只好等待。
  8. * 直到儿子把酱油买回来,妈妈才开始煮饭,最后终于把饭煮好。
  9. */
  10. public class Cooking {
  11.         //做饭场景
  12.         public static void main(String[] args) {
  13.                 // TODO Auto-generated method stub
  14.                 Thread mother = new Thread(new Mother());
  15.                 mother.start();
  16.         }

  17. }
复制代码

结果:
妈妈正在做饭。。。
妈妈发现没酱油了。。。
妈妈叫儿子去买啊酱油
儿子跑到了商店
儿子买到了酱油
儿子把酱油交给了妈妈。。。。
妈妈继续做饭
妈妈终于把饭做好了!!


重点:对于join的理解!
回复 使用道具 举报
看见有妹子我就进来了- -
回复 使用道具 举报
question5:
a,b,c去水房接水,他们在水龙头是排队接水的,谁先到达水龙头,谁先接到水
但是他们都接到水后不先喝,等其他人都接到水后再一起喝水。

persion类:
  1. package com.feng;

  2. public class Persion extends Thread {
  3.         private String stuName;
  4.         private Water water;
  5.         public String getStuName() {
  6.                 return stuName;
  7.         }
  8.         public void setWater(Water water) {
  9.                 this.water = water;
  10.         }
  11.         public Persion(String stuName) {
  12.                 this.stuName = stuName;
  13.         }
  14.         /**
  15.          * 人接水的一个过程
  16.          */
  17.         public void receivedWater() {
  18.                 System.out.println(stuName + "--->来到水房<-----");
  19.                 water.folwerWater(this);
  20.                 System.out.println(stuName + "--->正在喝水<-----");
  21.         }
  22.         /**
  23.          * 子线程运行
  24.          */
  25.         public void run() {
  26.                 // TODO Auto-generated method stub
  27.                 super.run();
  28.                 receivedWater();
  29.         }
  30. }
复制代码

water类:
  1. package com.feng;

  2. public class Water {
  3.         private int numbers;
  4.         private int i = 0;
  5.         public Water(int numbers) {
  6.                 this.numbers = numbers;
  7.         }
  8.         /**
  9.          * 水龙头流水的方法
  10.          * wait()与notifyAll()的理解:
  11.          *                 一般在同步代码块使用
  12.          *                 wait 使该子线程等待,直到被唤醒,该子线程才往下执行,否则则一直阻塞
  13.          *      notifyAll 唤醒所有在该线程中等待的线程 ,唤醒需要条件
  14.          * @param p
  15.          */
  16.         public synchronized void folwerWater(Persion p) {
  17.                 //定义同步代码块
  18.                 synchronized (this) {
  19.                         System.out.println(p.getStuName() + "--->正在接水");
  20.                         try {
  21.                                 Thread.sleep(3000);  //延时3m
  22.                         } catch (InterruptedException e) {
  23.                                 // TODO Auto-generated catch block
  24.                                 e.printStackTrace();
  25.                         }
  26.                         System.out.println(p.getStuName() + "--->接水结束");
  27.                         if(i < numbers-1) {
  28.                                 i++;
  29.                                 try {
  30.                                         wait();
  31.                                 } catch (InterruptedException e) {
  32.                                         // TODO Auto-generated catch block
  33.                                         e.printStackTrace();
  34.                                 }
  35.                         } else {
  36.                                 notifyAll();
  37.                         }
  38.                 }
  39.         }
  40. }
复制代码

测试类 :
  1. package com.feng;

  2. /**
  3. * a,b,c去水房接水,他们在水龙头是排队接水的,谁先到达水龙头,谁先接到水
  4. * 但是他们都接到水后不先和,等其他人都接到水后再一起喝水。
  5. * @author Administrator
  6. *
  7. */
  8. public class Test {

  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 Persion a = new Persion("A");
  12.                 Water water = new Water(3);
  13.                 a.setWater(water);
  14.                
  15.                 Persion b = new Persion("B");
  16.                 b.setWater(water);
  17.                
  18.                 Persion c = new Persion("C");
  19.                 c.setWater(water);
  20.                
  21.                 a.start();
  22.                 b.start();
  23.                 c.start();
  24.         }

  25. }
复制代码

结果如下:
A--->来到水房<-----
A--->正在接水
C--->来到水房<-----
B--->来到水房<-----
A--->接水结束
B--->正在接水
B--->接水结束
C--->正在接水
C--->接水结束
B--->正在喝水<-----
C--->正在喝水<-----
A--->正在喝水<-----


重点在与wait和notifyAll的理解!!!

回复 使用道具 举报
本帖最后由 冯超 于 2014-5-8 11:37 编辑

quest4:
四个学生,下课后往水房接水,由于水龙头只有一个,而且是排队接水,所以谁先到达水房,谁就能先喝水.

  1. <P>package com.feng.thread;</P>
  2. <P>/**
  3. * 单利模式设计
  4. * @author Administrator
  5. *
  6. */
  7. public class Water {
  8. private static Water water;
  9. private Water() {};
  10. /**
  11.   * 水龙头流水的过程
  12.   *   synchronized的理解:
  13.   *    同步:当多个线程访问同一个同步代码块 或者同步函数时候,哪个线程先进入这个同步代码快
  14.   *           或者同步函数,其他线程就必须等待,直到执行完为止!
  15.   * @param p
  16.   */
  17. public synchronized void flowerWater(Persion p) {
  18.   System.out.println("水龙头正在为---->" + p.getStuName() + "流水");
  19.   try {
  20.    Thread.sleep(3000);   //延时3 s
  21.   } catch (InterruptedException e) {
  22.    // TODO Auto-generated catch block
  23.    e.printStackTrace();
  24.   }
  25.   System.out.println("水龙头结束为---->" + p.getStuName() + "流水");
  26. }
  27. public static Water getInstence() {
  28.   if(water == null) {
  29.    water = new Water();
  30.   }
  31.   return water;
  32. }
  33. }</P>
  34. <DIV class=blockcode>
  35. <BLOCKQUOTE>package com.feng.thread;

  36. public class Persion extends Thread {
  37. private String stuName;
  38. private Water water;
  39. public String getStuName() {
  40. return stuName;
  41. }
  42. public void setWater(Water water) {
  43. this.water = water;
  44. }
  45. public Persion(String stuName) {
  46. this.stuName = stuName;
  47. }
  48. /**
  49. * 接水的过程
  50. */
  51. public void receiveWater() {
  52. System.out.println(stuName + "来到水房");
  53. water.flowerWater(this);
  54. System.out.println(stuName + "正在喝水");
  55. }
  56. //子线程执行的方法
  57. @Override
  58. public void run() {
  59. // TODO Auto-generated method stub
  60. super.run();
  61. receiveWater();
  62. }
  63. }
复制代码

测试类:
  1. package com.feng.thread;

  2. /**
  3. * 四个学生,下课后往水房接水,由于水龙头只有一个,而且是排队接水,所以谁先到达水龙头,谁就能先喝水.
  4. * @author Administrator
  5. *
  6. */
  7. public class Test {

  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. Persion a = new Persion("A");
  11. a.setWater(Water.getInstence());

  12. Persion b = new Persion("B");
  13. b.setWater(Water.getInstence());

  14. Persion c = new Persion("C");
  15. c.setWater(Water.getInstence());

  16. a.start();
  17. b.start();
  18. c.start();

  19. }

  20. }
复制代码

  1. <P>
  2. 结果如下:</P>
  3. <P> </P>
  4. <P>A来到水房
  5. B来到水房
  6. 水龙头正在为---->A流水
  7. C来到水房
  8. 水龙头结束为---->A流水
  9. A正在喝水
  10. 水龙头正在为---->C流水
  11. 水龙头结束为---->C流水
  12. C正在喝水
  13. 水龙头正在为---->B流水
  14. 水龙头结束为---->B流水
  15. B正在喝水</P>
复制代码

唯一的就是增加了同步代码块的理解:
synchronized
   指在多线程中,当多个子线程访问一个同步代码块或者同步函数的时候,哪个子线程先进入,其他子线程就必须等待,直到该同步代码块空闲,其他线程才能在进去.
回复 使用道具 举报
本帖最后由 冯超 于 2014-5-8 10:17 编辑

quest3:
四个学生
       A,B,C,D下课后往水房跑,在具体接水过程中
      由于水房只有一个水龙头,所以四个学生通过抢夺形式接水,因此谁先接完水不确定。


以面向对象的思想分析:
     学生   水龙头
过程:
学生    ----》水房     
水房中的水龙头 -----》流水

学生类:
  1. package com.feng.water;

  2. public class Person extends Thread{
  3.         private String stuName;
  4.         private Water water;
  5.         
  6.         public Person(String stuName) {
  7.                 this.stuName = stuName;
  8.         }
  9.         public String getStuName() {
  10.                 return stuName;
  11.         }
  12.         public void setStuName(String stuName) {
  13.                 this.stuName = stuName;
  14.         }

  15.         public Water getWater() {
  16.                 return water;
  17.         }
  18.         public void setWater(Water water) {
  19.                 this.water = water;
  20.         }
  21.         /**
  22.          * 人喝水的动作
  23.          */
  24.         public void receiveWater() {
  25.                 System.out.println(stuName + "---->跑向饮水机");
  26.                 water.flowWater(this);
  27.                 System.out.println(stuName + "------->正在喝水");
  28.         }
  29.         public void run() {
  30.                 // TODO Auto-generated method stub
  31.                 receiveWater();
  32.         }

  33.         
  34.         
  35. }
复制代码

水龙头:
  1. package com.feng.water;
  2. /**
  3. * 饮水机
  4. * @author fengchao
  5. *
  6. */
  7. class Water extends Thread {
  8.         /**
  9.          * 饮水机所具备的方法:流水
  10.          */
  11.         private static Water water;
  12.         private Water(){};
  13.         public void flowWater(Person p) {
  14.                 System.out.println(p.getStuName() + "来到了饮水机。。。。。");
  15.                 try {
  16.                         Thread.sleep(3000);   //休眠3m  
  17.                 } catch (InterruptedException e) {
  18.                         // TODO Auto-generated catch block
  19.                         e.printStackTrace();
  20.                 }
  21.                 System.out.println(p.getStuName()+ "已经正在接水。。。。。");
  22.         }
  23.         public static Water getInstence() {
  24.                 if(water == null) {
  25.                         water = new Water();
  26.                 }
  27.                 return water;
  28.         }
  29. }
复制代码
如何流水的方法没实现,用syso 代替!

测试类:
  1. package com.feng.water;

  2. /**
  3. * 四个学生
  4. *    A,B,C,D下课后往水房跑,在具体接水过程中
  5. *    由于水房只有一个水龙头,所以四个学生通过抢夺形式接水,因此谁先接完水不确定
  6. * @author fengchao
  7. *
  8. */
  9. public class First {
  10.         public static void main(String args[]) {
  11.                 Person a = new Person("A");
  12.                 //a.setName("A");
  13.                 a.setWater(Water.getInstence());
  14.                
  15.                 Person b = new Person("B");
  16.                 //b.setName("B");
  17.                 b.setWater(Water.getInstence());
  18.                
  19.                 Person c = new Person("C");
  20.                 //c.setName("C");
  21.                 c.setWater(Water.getInstence());
  22.                
  23.                 a.start();
  24.                 b.start();
  25.                 c.start();
  26.                
  27.                 //System.out.println(Water.getInstence() instanceof Water);

  28.         }
  29.                
  30. }
复制代码


结果:
B---->跑向饮水机
B来到了饮水机。。。。。
A---->跑向饮水机
A来到了饮水机。。。。。
C---->跑向饮水机
C来到了饮水机。。。。。
B已经正在接水。。。。。
B------->正在喝水
A已经正在接水。。。。。
C已经正在接水。。。。。
C------->正在喝水
A------->正在喝水


这个并是:多线程并发的一个过程。简称 不排队 接水的一个过程,谁先到达,与谁先接到水无必然关系,因为是不排队。

下章:排队接水    接完水一起喝水。敬请期待!
回复 使用道具 举报
哈哈哈,有妹子,为了妹子顶一个;P
回复 使用道具 举报
呵呵,有意思,支持一下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马