多线程

一.多线程的类别:

       sleep表示线程睡眠。单位毫秒。
   a.没设优先级的线程,执行的是并发的:

               线程间不存在synchronized关键字。

               线程间存在synchronized关键字:
                synchronized关键字主要保证一个线程在执行Synchronized方法或者代码块时,不被另外一个线程所打断。就是要完整的执行,而不是断断续续的。其实synchronized关键字就是运用对象锁的机制。
               它可以锁线程之间共同的代码块,也可以锁线程之间不是共同的代码块。用于没设优先级的一些线程。
               wait()方法,notify方法和notifyAll()只能用于synchronized关键字当中,用于有锁线程之间的通信。
              wait()被调用是,表明当前线程被中止,并且放弃对象锁。
              notify()唤醒的是具有锁的线程中的单个线程。
               notifyAll()被调用时,唤醒的是具有锁的线程中的多个线程,但是执行的是任意一个。

   b.设了优先级,那么优先级高的线程先执行,数字越大,优先级越高。yield表示线程让步,让步给优先级高的线程。

二.wait()方法是释放了对象锁,而sleep(()并没有释放对象锁。

三。java当中,静态方法加上锁,然后在非静态方法加上锁。其实这样它们是无法互斥的。所以需要这么做:

package cn.itcast.heima2;

public class TraditionalThreadSynchronized {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	
	private void init(){
		final Outputer outputer = new Outputer();
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					outputer.output("zhangxiaoxiang");
				}
				
			}
		}).start();
		
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					outputer.output3("lihuoming");
				}
				
			}
		}).start();
		
	}

	static class Outputer{
		
		public void output(String name){
			int len = name.length();
			synchronized (Outputer.class) 
			{
				for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}
		
		public synchronized void output2(String name){
			int len = name.length();
			for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
			}
			System.out.println();
		}
		
		public static synchronized void output3(String name){
			int len = name.length();
			for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
			}
			System.out.println();
		}	
	}
}

  这样,output与output3这两个方法就互斥了。

四.java多线程 典型案例:子线程执行10次,主线程执行100次,两者交替50次,如何实现:

  

public class Test {
    public static void main(String[] args) {
        final Business business = new Business();
        new Thread(new Runnable() {
            public void run() {
                for (int i = 1; i <= 50; i++) {
                    business.sub(i);
                }
            }
        }).start();
        for (int i = 1; i <= 50; i++) {
            business.main(i);
        }
    }
}

class Business {
    private boolean bShouldSub = true;

    public synchronized void sub(int i) {
        // 如果bussiness调用sub的话,则【!bShouldSub =false,bShouldSub=true】;
        // 然后主线程main为等待,执行完sub后就bShouldSub=false;唤醒主线程main。
        while (!bShouldSub) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int j = 1; j <= 10; j++) {
            System.out.println("sub thread sequence of " + j + ",loop of " + i);
        }
        bShouldSub = false;
        // 唤醒main
        this.notify();
    }

    public synchronized void main(int i) {
        while (bShouldSub) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        for (int j = 1; j <= 100; j++) {
            System.out
                    .println("main thread sequence of " + j + ",loop of " + i);
        }
        bShouldSub = true;
        // 唤醒sub
        this.notify();
    }
}

  

 

posted on 2019-05-25 11:56  芝麻开花——节节高  阅读(157)  评论(0编辑  收藏  举报

导航