您现在的位置是:首页 >

怎么实现多线程 强大的多线程和倒计时程序

火烧 2022-09-26 00:19:49 1075
强大的多线程和倒计时程序   多线程技术是非常实用的技术 特别是碰到有关运行多个任务的程序 就只有多线程才能满足你的要求 在以下程序中我所展示的是一个倒计时程序及利用Calla le接口 不是Ru a

强大的多线程和倒计时程序  

  多线程技术是非常实用的技术 特别是碰到有关运行多个任务的程序 就只有多线程才能满足你的要求 在以下程序中我所展示的是一个倒计时程序及利用Callable接口(不是Runnable接口)来返回一个你所要求的值 关于这方面的知识 你要多看一下API文档中的线程池类executor和callable接口 如果读者感兴趣的话以下的代码一定要认真的看 特别是我标了注释的地方 相信读者一定会有收获的 还可以把下面的代码复制下来自己去运行一下看一下结果 如果不懂的地方可以发到我的邮箱 本人很荣幸为您解答 只要我力所能及的事我一定会去帮助您!

  package rick_demo;

  import java awt *;

  import java awt event *;

  import java util Calendar;

  import ncurrent Callable;

  import ncurrent ExecutionException;

  import ncurrent ExecutorService;

  import ncurrent Executors;

  import ncurrent Future;

  import java util ArrayList;

  import javax swing *;

  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( );

  //ja append( hellon + world );

  jp =new JScrollPane(ja);

  jp setHorizontalScrollBarPolicy(JScrollPane HORIZONTAL_SCROLLBAR_ALWAYS);

  jp setVerticalScrollBarPolicy(JScrollPane VERTICAL_SCROLLBAR_ALWAYS);

  for(int i= ;i< ;i++)

  str[i] = new String();

  jf setText( 单击按纽进行相应的操作 );

  jButton = new JButton( 显示时间剩余时间 );

  jButton = new JButton( 暂停 );

  Container container = getContentPane();

  container setLayout(new FlowLayout());

  container add(jf );

  container add(jButton );

  container add(jButton );

  container add(jp);

  //获取系统刚开始运行的初始值

  now = Calendar getInstance();

  hourEnd = now get(Calendar HOUR_OF_DAY) + ;

  minutePlus = now get(Calendar MINUTE);

  secondPlus = now get(Calendar SECOND);

  t = new Thread(new Runnable() {

  public void run() {

  while (true) {

  while (right) {

  try {

  Thread sleep( );

  jf setText(CalTime());

  } catch (InterruptedException e) {

  e printStackTrace();

  }

  }

  }

  }

  });

  t start();

  jButton addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent event) {

  right = true;

  }

  });

  jButton addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent g) {

  right = false;

  }

  });

  setVisible(true);

  }

  public String CalTime() {

  now = Calendar getInstance();

  hour = now get(Calendar HOUR_OF_DAY);

  minute = now get(Calendar MINUTE);

  second = now get(Calendar SECOND);

  if (hourEnd >= hour) {

  hourend = hourEnd hour;

  secondend = second+secondPlus;

  minutend = minute+minutePlus;

  }

怎么实现多线程 强大的多线程和倒计时程序

  return 小时倒计时hour: + hourend + minute: + minutend

  + second: + secondend;

  }

  public String[] work(){ //多线程关键代码

  ExecutorService exec= Executors newCachedThreadPool();

  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;

  }

lishixinzhi/Article/program/Java/gj/201311/27534  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码