多线程

多线程


1线程与进
进程是程序的一次动态执行过程

线程和进程一样,都是实现并发的一个基本单单位。线程是比进程更小的执行单位。
多线程是实现并发机制的一种有效手段。
举例:在使用word的时候会有拼写检查,那么word程序就是进程,拼写检查就是线程,
此时除了拼写检查,在word进程上还有其他线程在运行。关闭线程进程不会停,关闭进程线程会随之关闭。


2 java中线程的实现
 继承thread类或者实现Runnable接口
 继承thread类:
    子类继承thread类的run()方法,此方法为线程的主体
    注意 1因为线程的运行需要本机操作系统的支持,所以要用start()(不妨看看start()的代码)
    2有的时候运行出来的效果看不出是多线程的,我觉得应该是任务量太少,一下子就运行完了导致。
        3对象只能调用一次start()
		
package mjc;


class MyThread extends Thread{
	private String name;
	public MyThread(String name){
		this.name=name;
	}
	public void run(){
		for(int i=0;i<100;i++){
			System.out.println(name+" runing,I="+i);
		}
	}
}




public class ThreadDemo{
	public static void main(String args[]){
		MyThread myThread1=new MyThread("线程A");
		MyThread myThread2=new MyThread("线程B");
		myThread1.start();//此处应该start()方法来启动线程,而不能直接调用run()方法
		myThread2.start();//虽然调用的是start()方法,实际上调用的是run()方法主体	
	}
}
	



 实现Runnable接口
    Runnable接口没有start()方法,依然依靠Thread才能启动
thread类中提供了两个构造方法接收runnable的实例对象

package mjc;


class MyRunnable implements Runnable{
	private String name;
	public MyRunnable(String name){
		this.name=name;
	}
	public void run(){
		for(int i=0;i<100;i++){
			System.out.println(name+" runing,I="+i);
		}
	}
}


public class ThreadDemo{
	public static void main(String args[]){
		MyRunnable my1=new MyRunnable("实现接口的线程A ");
		MyRunnable my2=new MyRunnable("实现接口的线程B ");
		Thread t1=new Thread(my1);
		Thread t2=new Thread(my2);
		t1.start();
		t2.start();
	}
}
	



 两者的区别
    Runnable 适合多个线程共享资源,而Thread不能
比如卖五张票,(票是资源)runnable有三个线程,会一共卖五张,thread有三个线程,会各自卖五张。


3线程的状态:创建、就绪、运行、阻塞、终止


4线程操作的相关方法
   取得和设置线程名称getName() setName()
   判断线程是否启动isAlive()
   线程的强制运行join()强制运行期间,其他线程无法运行,需要等待
   线程的休眠sleep()
   线程中断interrupt()
   后台线程setDaemon(),java进程结束后,后台线程会继续执行
   线程的优先级
   线程的礼让yield()
   
   
   Demo演示
   创建三个线程,分别休眠(两种方法)
package mjc;


class Mythread extends Thread{
	private int time;
	public Mythread(String name,int time){
		super(name);
		this.time=time;
	}


	public void run(){
		try {
			Thread.sleep(this.time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName()+"线程,休眠"+this.time);
	}
}
//以上程序使用了Thread类的特点,所以不用单独设置name属性




<span style="font-size:18px;">class MyRunnable implements Runnable{
	private String name;
	private int time;
	public MyRunnable(String name ,int time){
		this.name=name;
		this.time=time;
	}
	public void run(){
		try {
			Thread.sleep(this.time);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(this.name+"线程,休眠"+this.time);
	}
}
public class ThreadDemo{
	public static void main(String args[]){
		Mythread t1=new Mythread("线程A",1000);
		Mythread t2=new Mythread("线程B",2000);
		Mythread t3=new Mythread("线程C",3000);
		t1.start();
		t2.start();
		t3.start();
		
		
		MyRunnable r1=new MyRunnable("线程1",1000);
		MyRunnable r2=new MyRunnable("线程2",2000);
		MyRunnable r3=new MyRunnable("线程3",3000);
		new Thread(r1).start();
		new Thread(r2).start();
		new Thread(r3).start();
	}
}</span>








同步问题:当有休眠的时候,操作同一资源会出错,此时使用synchronized(同步对象){需要同步的代码}进行同步操作
原理:当一个进程执行完后执行下一个进程
<span style="font-size:18px;">package mjc;


class MyRunnable implements Runnable{
private int ticket=5;
	public void run(){
		for(int i=0;i<10;i++){
			synchronized (this) {
			 if(ticket>0){		
				try {
					Thread.sleep(300);
				} catch (InterruptedException e) { 
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("买票"+ticket--);
		   	 }	
		   }
		}
	}
}
public class ThreadDemo{
	public static void main(String args[]){


		MyRunnable mt=new MyRunnable();
		Thread mt1=new Thread(mt);
		Thread mt2=new Thread(mt);
		Thread mt3=new Thread(mt);
		mt1.start();
		mt2.start();
		mt3.start();


	}
}</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值