日期:2014-05-20 浏览次数:20777 次
public class Machine implements Runnable{ static StringBuffer log = new StringBuffer(); public void run() { // TODO Auto-generated method stub int count =0; for(int i=0;i<20;i++){ log.append("m"+i); if(++count%10==0){log.append("\n");} } } }
public class Test implements Runnable{ static StringBuffer log = new StringBuffer(); public void run() { // TODO Auto-generated method stub int count =0; for(int i=0;i<20;i++){ log.append(Thread.currentThread().getName()+":m"+i); if(++count%10==0){log.append("\n");} } } public static void main(String[] args) throws InterruptedException { Machine machine1 = new Machine(); Machine machine2 = new Machine(); Thread t1 = new Thread(machine1); Thread t2 = new Thread(machine2); t1.setName("t1"); t2.setName("t2"); Thread main = Thread.currentThread(); main.setPriority(3); System.out.println("主线程的优先级是"+main.getPriority()); System.out.println("线程t1的优先级是"+t1.getPriority()); System.out.println("线程t2的优先级是"+t2.getPriority()); t1.setPriority(Thread.MIN_PRIORITY); t2.setPriority(Thread.MAX_PRIORITY); t1.start(); t2.start(); Thread.sleep(500); System.out.println(log); } }