zoukankan      html  css  js  c++  java
  • Java多线程入门Ⅱ

    线程的让步

    线程让出自己占用的CPU资源

    • 线程让出资源,不指定让给谁
    • 线程让出资源,指定让给谁

    方法1:

    public static void yield();
    

    线程实现交替打印

    import java.io.*;
    import java.util.*;
    class MyRunnable implements Runnable{
    	private String l;
    	private String r;
    	public MyRunnable(String fl,String fr) {
    		this.l=fl;this.r=fr;
    	}
    	public void run() {
    		for(int i=0;i<30;i++) {
    			System.out.print(l+" "+i+" "+r+" ");
    			Thread.yield();
    		}
    	}
    }
    public class Main {
        public static void main(String[] args) {
            MyRunnable mr1=new MyRunnable("[","]");
            MyRunnable mr2=new MyRunnable("(",")");
            Thread t1=new Thread(mr1);
            Thread t2=new Thread(mr2);
            t1.start();t2.start();
        }
    }
    

    方法2:

    public final void join() throws InterruptedException 
    public final void join(long millis) throws InterruptedException 
    public final void join(long millis,int nano) throws InterruptedException 
    

    将两个线程合并为同一个线程,A调用join B,A等到B结束再恢复执行

    import java.io.*;
    import java.util.*;
    class MyRunnable implements Runnable{
    	private String l;
    	private String r;
    	public MyRunnable(String fl,String fr) {
    		this.l=fl;this.r=fr;
    	}
    	public void run() {
    		for(int i=0;i<30;i++) {
    			System.out.print(l+" "+i+" "+r+" ");
    			Thread.yield();
    		}
    	}
    }
    public class Main {	
        public static void main(String[] args) {
            MyRunnable mr1=new MyRunnable("[","]");
            Thread t1=new Thread(mr1);
            t1.start();
            for(int i=0;i<30;i++) {
            	if(i==10) {
            		try {
            			System.out.print("Join");
            			t1.join();
            		}catch(InterruptedException e) {
            	        e.printStackTrace();		
            		}
            	}
            	System.out.print("( "+i+" )");
            }
        }
    }
    

    方法3:

    public final void setDaemon(boolean on) //将某个线程设置为守护线程
    

    方法4:

    public static void sleep(long millis)throws InterruptedExcepiton
    

    线程同步


    方法1:

    synchronized 加锁
    
    class Resources{
    	synchronized void function1(Thread currThread) {
    		for(int i=0;i<300;i++) {
    			System.out.println(currThread.getName());
    		}
    	}
    }
    class MyThread extends Thread{
    	Resources rs;
    	public MyThread(String tname,Resources trs) {
    		this.setName(tname);
            this.rs=trs;
    	}
    	public void run() {
    		if(this.getName().equals("Thread1")) {
    			System.out.println("Thread1启动,等待进入function1");
    			rs.function1(this);
    		}else {
    			System.out.println("Thread2启动,等待进入function1");
    			rs.function1(this);
    		}
    	}
    }
    public class Main {
        public static void main(String[] args) {
            Resources rs=new Resources();
            MyThread t1=new MyThread("Thread1",rs);
            MyThread t2=new MyThread("Thread2",rs);
            t1.start();t2.start();
        }
    }
    

    方法2:

    public final void wait() throws InterruptedException
    public final void wait(long timeout) throws InterruptedException
    public final void notify()
    public final void notifyAll()
    
  • 相关阅读:
    js原型对象与Java类的比较
    javascript特效--制作背景电子钟(整点时祝贺生日快乐)
    Java web MVC开发模式入门感悟
    重新审视面向对象编程
    10-排序4 统计工龄(20 分)
    基数排序
    表排序
    快速排序
    09-排序3 Insertion or Heap Sort(25 分)
    09-排序2 Insert or Merge(25 分)
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/7640454.html
Copyright © 2011-2022 走看看