多线程技术是非常实用的技术特别是碰到有关运行多个任务的程序就只有多线程才能满足你的要求在以下程序中我所展示的是一个倒计时程序及利用Callable接口(不是Runnable接口)来返回一个你所要求的值关于这方面的知识你要多看一下API文档中的线程池类executor和callable接口如果读者感兴趣的话以下的代码一定要认真的看特别是我标了注释的地方相信读者一定会有收获的还可以把下面的代码复制下来自己去运行一下看一下结果如果不懂的地方可以发到我的邮箱本人很荣幸为您解答只要我力所能及的事我一定会去帮助您! package rick_demo; import javaawt*; import javaawtevent*; import javautilCalendar; import ncurrentCallable; import ncurrentExecutionException; import ncurrentExecutorService; import ncurrentExecutors; import ncurrentFuture; import javautilArrayList; import javaxswing*; public class Test extends JFrame { private static final long serialVersionUID = L; private String[] str=new String[]; String bin = new String(); JTextField jf; JTextArea ja; JScrollPane jp; JButton jButton; private static short coin=; JButton jButton; Calendar now; int hour; int hourEnd; int minute; int minutend; int second; int secondend; int hourend; int minutePlus; int secondPlus; Thread t; boolean right = true; public Test() { setSize( ); jf = new JTextField(); ja = new JTextArea(); //jaappend(hello\n+world); jp =new JScrollPane(ja); jpsetHorizontalScrollBarPolicy(JScrollPaneHORIZONTAL_SCROLLBAR_ALWAYS); jpsetVerticalScrollBarPolicy(JScrollPaneVERTICAL_SCROLLBAR_ALWAYS); for(int i=;i<;i++) str[i] = new String(); jfsetText(单击按纽进行相应的操作); jButton = new JButton(显示时间剩余时间); jButton = new JButton(暂停); Container container = getContentPane(); containersetLayout(new FlowLayout()); containeradd(jf ); containeradd(jButton); containeradd(jButton); containeradd(jp); //获取系统刚开始运行的初始值 now = CalendargetInstance(); hourEnd = nowget(CalendarHOUR_OF_DAY) + ; minutePlus = nowget(CalendarMINUTE); secondPlus = nowget(CalendarSECOND); t = new Thread(new Runnable() { public void run() { while (true) { while (right) { try { Threadsleep(); jfsetText(CalTime()); } catch (InterruptedException e) { eprintStackTrace(); } } } } }); tstart(); jButtonaddActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { right = true; } }); jButtonaddActionListener(new ActionListener() { public void actionPerformed(ActionEvent g) { right = false; } }); setVisible(true); } public String CalTime() { now = CalendargetInstance(); hour = nowget(CalendarHOUR_OF_DAY); minute = nowget(CalendarMINUTE); second = nowget(CalendarSECOND); if (hourEnd >= hour) { hourend = hourEnd hour; secondend = second+secondPlus; minutend = minute+minutePlus; } return 小时倒计时hour: + hourend + minute: + minutend + second: + secondend; } public String[] work(){ //多线程关键代码 ExecutorService exec= ExecutorsnewCachedThreadPool(); ArrayList> results= new ArrayList>(); for(int i=0;i<10;i++) results.add(exec.submit(new TaskT(i))); for(Future fuck : results) try{ bin = fuck.get() ; str[coin] = bin; coin++; } catch(InterruptedException e){ System.exit(1); } catch(ExecutionException e){ } exec.shutdown(); return str; } public void setStr(String[] alph){ for(int i=0;i ja.append(alph[i]+"\n"); } public static void main(String args[]) { Test sleep = new Test(); sleep.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); sleep.setStr(sleep.work()); } } class TaskT implements Callable{ private int id=0; public TaskT(int id){ this.id=id; } public String call(){ return "TaskT "+id; } } |