熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

強大的多線程和倒計時程序

2022-06-13   來源: Java高級技術 

  多線程技術是非常實用的技術特別是碰到有關運行多個任務的程序就只有多線程才能滿足你的要求在以下程序中我所展示的是一個倒計時程序及利用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;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27534.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.