java线程——Eclipse开发

来源:互联网 发布:完颜撒离喝 知乎 编辑:程序博客网 时间:2024/06/15 22:17

基本原则:不要忘了同步。了解线程如何交互、防止线程互相干扰。

线程分两种:1、守护程序线程。例如垃圾回收机制,通过setDeamon()方法可设置某线程为守护; 2、非守护程序线程。当所有非守护运行结束,java程序就结束运行。(守护不一定结束)

避免死锁:确保在获取多个锁时,在所有的线程中都以相同的顺序获取锁。

同步(synchronized)准则:1、使代码块保持简短;2、不要阻塞;3、在持有锁的时候,不要对其它对象调用方法。

大多数程序应该完全避免更改线程优先级。

import java.util.Random;

public class TenThreads {

    
public static class WorkerThread extends Thread {
        
int max = Integer.MIN_VALUE;

        
int[] ourArray;

        
public WorkerThread(int[] ourArray) {
            super();
            
this.ourArray = ourArray;
        }

        
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) {
        WorkerThread threads[] 
= new WorkerThread[10];
        
int[][] bigMatrix = getBigHairyMatrix();
        
int max = Integer.MIN_VALUE;

        
for (int i = 0; i < 10; i++) {
            threads[i] 
= new WorkerThread(bigMatrix[i]);
            threads[i].start();
        }

        
try {
            
for (int i = 0; i < 10; i++) {
                
//join()方法,当前线程阻塞,直到调用线程(threads[i])运行结束
                threads[i].join();
                System.
out.println("threads["+i+"]= " + threads[i].getMax());
                max 
= Math.max(max,threads[i].getMax());
            }
        } 
catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.
out.println("Max = " + max);
    }

    
public static int[][] getBigHairyMatrix() {
        
int[][] matrix = new int[10][10];
        
try{
        
for (int i = 0; i < 10; i++) {
            
for (int j = 0; j < 10; j++) {
                matrix[i][j] 
= Random.class.newInstance().nextInt(100);
                System.
out.println("matrix["+i+"]["+j+"] = " + matrix[i][j]);
            }
        }}
catch(Exception e){}
        
return matrix;
    }

}

 

import java.util.Timer;
//可以稍后在某个时间执行任务,或定期执行任务
import java.util.TimerTask;

public class CalculatePrimes extends Thread {

    
public static final int MAX_PRIMES = 1000000;

    
public static final int TEN_SECONDS = 10000;

    
public volatile boolean finished = false;

    
public void run() {
        
// TODO run方法
        int[] primes = new int[MAX_PRIMES];
        
int count = 0;
        
for (int i = 2; i < MAX_PRIMES; i++) {
            
if (finished) {
                
break;
            }
            
boolean prime = true;
            
for (int j = 0; j < count; j++) {
                
if (i % primes[j] == 0) {
                    prime 
= false;
                    
break;
                }
            }
            
if (prime) {
                primes[count
++= i;
                System.out.println(
"Found Prime: " + i);
            }
        }
    }

    
public static void main(String[] args) {
        
// TODO main()方法
        Timer timer = new Timer();

        
final CalculatePrimes calculator = new CalculatePrimes();
        calculator.start();
        
// //设置为守护程序线程。java程序是在其所有非守护程序线程运行结束后退出的,守护程序依然可以执行。
        
// calculator.setDaemon(true);

        
// try {
        
// Thread.sleep(TEN_SECONDS);
        
// } catch (InterruptedException e) {
        
// e.printStackTrace();
        
// }
        
// calculator.finished = true;

        timer.schedule(
new TimerTask() {
            
public void run() {
                calculator.finished 
= true;
            }
        }, TEN_SECONDS);
    }

}
原创粉丝点击