发布时间 : 星期日 文章(完整word版)Java知识总结完整版,推荐文档更新完毕开始阅读42c9f84f148884868762caaedd3383c4ba4cb456
public void run(){ while(flag){
System.out.println(\ try {
sleep(1000);
} catch (InterruptedException e) { return; } } } } join
合并某个线程(类似于调用方法,方法调用结束后,继续执行主函数)
public class TestJoin {
public static void main(String[] args) { MyThread2 t1 = new MyThread2(\ t1.start();
try {t1.join();}
catch (InterruptedException e) {} for(int i=1;i<=10;i++){
System.out.println(\ } } }
class MyThread2 extends Thread { MyThread2(String s){super(s);} public void run(){
for(int i =1;i<=10;i++){
System.out.println(\ try {sleep(1000);}
catch (InterruptedException e) {return;} } } }
yield
让出CPU,给其他线程执行的机会
public class TestYield {
public static void main(String[] args) { MyThread3 t1 = new MyThread3(\ MyThread3 t2 = new MyThread3(\
t1.start(); t2.start(); } }
class MyThread3 extends Thread { MyThread3(String s){super(s);} public void run(){
for(int i =1;i<=100;i++){
System.out.println(getName()+\ if(i==0){yield();} } } }
线程的优先级别
Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照线程的优先级决定应调度哪个线程来执行 线程的优先级用数字表示,范围从1到10,缺省为5 Thread.MIN_PRIORITY = 1 Thread.MAX_PRIORITY = 10 Thread.NORM_PRIORITY = 5
使用下述方法获得或设置线程对象的优先级 int getPriority()
void setPriority(int newPriority)
public class TestPriority {
public static void main(String[] args) { Thread t1 = new Thread(new T1()); Thread t2 = new Thread(new T2());
t1.setPriority(Thread.NORM_PRIORITY + 3); t1.start(); t2.start(); } }
class T1 implements Runnable { public void run() {
for(int i=0; i<1000; i++) {
System.out.println(\ } } }
class T2 implements Runnable { public void run() {
for(int i=0; i<1000; i++) {
System.out.println(\
} } }
线程同步
public class TestSync implements Runnable { Timer timer = new Timer();
public static void main(String[] args) { TestSync test = new TestSync(); Thread t1 = new Thread(test); Thread t2 = new Thread(test);
t1.setName(\ t1.start(); t2.start(); }
public void run(){
timer.add(Thread.currentThread().getName()); } }
class Timer{
private static int num = 0;
public /*synchronized*/void add(String name){ //synchronized (this) { num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+\你是第\个使用timer的线程\ //} } }
在java语言中,引入了对象互斥锁的概念,保证共享数据操作的完整性。每个对象都对应于一个可称为“互斥锁”的标记,这个标记保证在任意时刻,只能有一个线程访问该对象
关键字synchronized来与对象的互斥锁联系。当某个对象synchronized修饰时,表明该对象在任意时刻只能由一个线程访问(锁定的只是该方法内的代码)
生产者消费者问题 public class P2C {
public static void main(String[] args) { LanZi lz = new LanZi();
Producer p =new Producer(lz); Consumer c =new Consumer(lz); //new Thread(p).start(); //new Thread(p).start(); new Thread(p).start();
new Thread(c).start(); } }
class ManTou { int id;
ManTou(int id) {this.id = id;}
Object类中 public String toString() {return \public final void wait() throws }
InterruptedException class LanZi {
在其他线程调用此对象的 notify() 方 int index = 0;
法或 notifyAll() 方法前,导致当前线 ManTou[] arrMT = new ManTou[6];
程等待。 public synchronized void push(ManTou mt) { synchronized (obj) { while(index == arrMT.length) { while (
obj.wait(); catch (InterruptedException e){ ... // Perform action appropriate to e.printStackTrace();}
condition }
} this.notifyAll();
arrMT[index] = mt;
public final void notify() index ++;
唤醒在此对象监视器上等待的单个线 }
程。如果所有线程都在此对象上等待, public synchronized ManTou pop() { 则会选择唤醒其中一个线程。选择是 while(index == 0) {
任意性的,并在对实现做出决定时发 try {this.wait();}
生。线程通过调用其中一个 wait 方 catch (InterruptedException e){ 法,在对象的监视器上等待。 e.printStackTrace();}
直到当前线程放弃此对象上的锁定, }
才能继续执行被唤醒的线程。 this.notifyAll(); index --;
return arrMT[index]; } }
class Producer implements Runnable { LanZi lz = null;
Producer(LanZi lz) {this.lz = lz;} public void run() {
for(int i=0;i<20;i++) {
ManTou mt = new ManTou(i); lz.push(mt);
System.out.println(\生产了:\
try{Thread.sleep((int)(Math.random() * 200));} catch (InterruptedException e){ e.printStackTrace();} }