专注于Jsp开发,为Jsp开发提供源动力 VM主机| 海外空间| JAVA网站建设| 郑州网站建设| 网站优化| 郑州网络公司| 洛阳网站建设
jsp空间

Java线程操作实例

添加时间:[2010-7-7 18:24:04] 

实例一:10秒钟之内循环输出

  package theardtest;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  /**

  *

  * @author Wei.Liu

  */

  public class Main extends Thread {

  /**

  * @param args the command line arguments

  */

  public volatile boolean finished = false;

  public void run(){

  int i=0;

  while(!finished){

  System.out.println(i);

  i++;

  }

  }

  public static void main(String[] args) {

  // TODO code application logic here

  Main temp = new Main();

  temp.start();

  try {

  Thread.sleep(10000);//

  } catch (InterruptedException ex) {

  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

  }

  temp.finished=true;

  }

  实例二:开10个线程寻找2维数组中的最大值

  package threadtest;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  /**

  *

  * @author Wei.Liu

  */

  public class Main {

  static class WorkThread extends Thread{

  int max=Integer.MIN_VALUE;

  int [] ourArray;

  public void setArray(int [] ourArray){

  this.ourArray = ourArray;

  }

  @Override

  public void run(){

  for(int i=0;i<ourArray.length;i++){

  max = Math.max(max, ourArray[i]);

  }

  }

  public int getMax(){

  return max;

  }

  }

  public static void main(String [] args){

  WorkThread [] temp =new WorkThread[10];

  int [][]bigMatrix = {{2,12,5,9,42,12,5,9,4,12},{5,25,895,632,2},{111,0,15,45},{56,2},{4525,2,1},{12,12,12},{1,21,3},{12,1,2,3},{45,251,25,4},{15421524,251,458,26542,4865221,5624}};

  int max = Integer.MIN_VALUE;

  for(int i=0;i<10;i++){

  temp[i]= new WorkThread();

  temp[i].setArray(bigMatrix[i]);

  temp[i].start();

  }

  //Wait for each thread to finished

  try{

  for(int i=0;i<10;i++){

  temp[i].join();

  max = Math.max(max, temp[i].getMax());

  }}catch(Exception e){

  e.printStackTrace();

  }

  System.out.println("Max value:::"+max);

  }

  }

关于互斥锁的使用。

  package threadtest;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  /**

  *

  * @author Wei.Liu

  */

  public class Main {

  static volatile int x = 0;

  static volatile int y = 0;

  static class Thread1 extends Thread {

  int change = 0;

  public Thread1(int chage) {

  this.change = chage;

  }

  @Override

  public void run() {

  for (int i = 0; i < 20; i++) {

  x = change;

  try {

  Thread.sleep(100);

  } catch (InterruptedException ex) {

  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

  }

  y = change;

  System.out.println("x:" + x + " y:" + y);

  }

  }

  }

  public static void main(String[] args) {

  Thread temp1 = new Thread1(1111);

  Thread temp2 = new Thread1(2222);

  temp1.start();

  temp2.start();

  }

  }

  输出结果里X,Y的值多次出现不同,就是因为两个线程都在对X,Y的值进行修改造成的。

  在修改X,Y值的地方加上一个互斥锁,这样就可以保证每次输出时X,Y的值都是一致的了。

  package threadtest;

  import java.util.logging.Level;

  import java.util.logging.Logger;

  /**

  *

  * @author Wei.Liu

  */

  public class Main {

  static volatile int x = 0;

  static volatile int y = 0;

  static class Thread1 extends Thread {

  private static Object lockObject = new Object();

  int change = 0;

  public Thread1(int chage) {

  this.change = chage;

  }

  @Override

  public void run() {

  for (int i = 0; i < 20; i++) {

  synchronized (lockObject) {

  x = change;

  try {

  Thread.sleep(100);

  } catch (InterruptedException ex) {

  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

  }

  y = change;

  System.out.println("x:" + x + " y:" + y);

  }

  }

  }

  }

  public static void main(String[] args) {

  Thread temp1 = new Thread1(1111);

  Thread temp2 = new Thread1(2222);

  temp1.start();

  temp2.start();

  }

  }

关于我们 | 付款方式 | 客户管理 | 友情链接 | 网站导航 | 在线地图


版权所有 2008 三易网络(洛阳)科技开发有限公司 京ICP备06012028号

服务热线:0371-63653120 63658758(郑州) 0379-63921200   63265368(洛阳)

QQ在线客服: JSP空间咨询   JSP空间咨询    Email:web@suneasy.cn

郑州网络公司 郑州网站建设 洛阳网站建设

总部地址:纱厂南路41号中泰新城泰福苑803室 郑州分公司地址:金水区圣菲城