java基础之多线程

java线程

 
//没有实现资源共享的多线程
public class TestThread {
 
    /**
     * 1,多线程:①继承Thread类 ②实现Runnable接口
     * 2,
     * @param args
     */
    public static void main(String[] args) {
 
        Thread firstThread = new FirstThread();//创建线程对象
        firstThread.start();//启动子线程
 
        //主线程
        for(int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}
 
//继承Thread类
class FirstThread extends Thread{
 
    /**
     * 线程体在run()方法中
     */
    @Override
    public void run() {
            for(int i=0;i<100;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
}
 
 
 
//实现资源共享的多线程
//第一种方法
public class TestThread2 {
 
    int i = 0;
 
    public static void main(String[] args) {
 
        TestThread2 threadThread = new TestThread2();
 
        FirstThread1 thread1 = new FirstThread1("thread1",threadThread);
        Thread thread2 = new FirstThread1("thread2",threadThread);
 
        thread1.start();
        thread2.start();
    }
 
}
 
class FirstThread1 extends Thread{
 
 
    private TestThread2 testThread2;
 
    public FirstThread1(String name,TestThread2 testThread2) {
        super(name);
        this.testThread2 = testThread2;
    }
 
    //线程体在run()方法中
    @Override
    public void run() {
            for(;testThread2.i<100;testThread2.i++){
                //子线程和主线程共享一个i
            System.out.println(Thread.currentThread().getName()+":"+testThread2.i);
        }
    }
}
 
 
//第二种方法:实现Runnable接口(实现多线程资源共享的理想方法)
/**
* 1),创建实现Runnable接口的类(必须实现run()方法)
* 2),创建实现类的对象
* @author 话伤
*
*/
public class printNumber implements Runnable{
 
    int i=0;
    //必须实现run()方法
    @Override
    public void run() {
        for(;i<50;i++){
 
            System.out.println(Thread.currentThread().getName()+":"+i);
        }
    }
    public static void main(String[] args) {
        //创建实现类的对象
        printNumber print = new printNumber();
 
        Thread thread1 = new Thread(print);
        Thread thread2 = new Thread(print);
 
        //启动线程
        thread1.start();
        thread2.start();
    }
}
 
 
package com.cyq.testThread;
 
//两个线程交替打印字母
public class printLetters implements Runnable{
 
    private char ch = 'a';
 
    public synchronized void print(){
 
            System.out.println(Thread.currentThread().getName()+": "+ch);
            ch++;
            notify();//打印完之后,唤醒在等待的线程
            try {
                //当前线程打印完之后进行等待,让第二个线程执行
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
        }
    }
 
    @Override
    public void run() {
        while(ch <= 'z')
        print();
    }
 
    public static void main(String[] args) {
 
        Runnable runnable = new printLetters();
 
        Thread th1 = new Thread(runnable);
        Thread th2 = new Thread(runnable);
 
        //设置线程的名字
        th1.setName("线程-1");
        th2.setName("线程-2");
 
        th1.start();
        th2.start();
 
    }
 
}
 
对于多线程的理解:
    将Cup想成按时间顺序完成一个大任务,其中大任务又分成许多小任务,这些任务又由若干个线程去完成(线程都很积极的去抢着做任务),在每一个很小的时间片段里,Cup只能执行一个任务,也就是只有那个抢到任务的线程才能执行任务,wait()方法能让当前正在执行任务的线程出于等待状态,假如执行某项大任务的时候,当前线程甲正处于执行状态,遇到wait(),然后甲就必须放下手里的工作,把做任务的权利让出来,再由其他线程去抢任务来做,当遇到notify()方法时,刚刚出于等待状态的甲线程又会被唤醒,继续执行任务。(还一个notifyAll()方法,作用是唤醒所有出于等待状态的线程)
 
 
posted @ 2017-03-18 03:01  话伤  阅读(105)  评论(0)    收藏  举报