java多线程学习

首先讲一下进程和线程的区别:

进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程。

线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器(PC),线程切换开销小。

线程和进程一样分为五个阶段:创建、就绪、运行、阻塞、终止。

多进程是指操作系统能同时运行多个任务(程序)。

多线程是指在同一程序中有多个顺序流在执行。

在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口。

一、扩展java.lang.Thread类

 1 public class threadTest extends Thread {
 2     private String name;
 3     public threadTest(String name){
 4         this.name = name;
 5     }
 6     public void run(){
 7         for(int i = 0;i<=1000;i++){
 8             System.out.println(name+"  "+i);
 9             try {
10                 sleep((int) Math.random()*10);
11             } catch (InterruptedException e) {
12                 e.printStackTrace();
13             }
14         }
15     }
16 }
17 
18 class main{
19     public static void main(String[] args) {
20         threadTest test = new threadTest("小明");
21         threadTest test2 = new threadTest("小洪");
22         test.start();
23         test2.start();
24     }
25 }

输出:

小明  0
小洪  0
小明  1
小明  2
小明  3
小明  4
小明  5
小洪  1
小洪  2
小洪  3
小洪  4
小洪  5

说明:

程序启动运行main时候,java虚拟机启动一个进程,主线程main在main()调用时候被创建。随着调用MitiSay的两个对象的start方法,另外两个线程也启动了,这样,整个应用就在多线程下运行。

注意:start()方法的调用后并不是立即执行多线程代码,而是使得该线程变为可运行态(Runnable),什么时候运行是由操作系统决定的。

从程序运行的结果可以发现,多线程程序是乱序执行。因此,只有乱序执行的代码才有必要设计为多线程。

Thread.sleep()方法调用目的是不让当前线程独自霸占该进程所获取的CPU资源,以留出一定时间给其他线程执行的机会。

实际上所有的多线程代码执行顺序都是不确定的,每次执行的结果都是随机的。

但是start方法重复调用的话,会出现java.lang.IllegalThreadStateException异常。

如下:

1 class main{
2     public static void main(String[] args) {
3         threadTest test = new threadTest("小明");
4         threadTest test2 = new threadTest("小洪");
5         test.start();
6         test.start();
7     }
8 }

二、实现java.lang.Runnable接口

 1 public class RunableImp implements Runnable {
 2     private String name;
 3     public RunableImp(String name) {
 4         this.name = name;
 5     }
 6     
 7     @Override
 8     public void run() {
 9         for(int i=0;i<10;i++){
10             System.out.println(name+":  "+i);
11             try {
12                 Thread.sleep((int)Math.random()*10);
13             } catch (InterruptedException e) {
14                 e.printStackTrace();
15             }
16         }
17     }
18 }
19 
20 class main1{
21     public static void main(String[] args) {
22         RunableImp imp = new RunableImp("A");
23         RunableImp imp2 = new RunableImp("B");
24         Thread thread = new Thread(imp);
25         Thread thread2 = new Thread(imp2);
26         thread.start();
27         thread2.start();
28     }
29 }

输出:

A:  0
A:  1
A:  2
A:  3
A:  4
A:  5
A:  6
A:  7
A:  8
A:  9
B:  0
B:  1
B:  2
B:  3
B:  4
B:  5
B:  6
B:  7
B:  8
B:  9

说明:

Thread2类通过实现Runnable接口,使得该类有了多线程类的特征。run()方法是多线程程序的一个约定。所有的多线程代码都在run方法里面。Thread类实际上也是实现了Runnable接口的类。

在启动的多线程的时候,需要先通过Thread类的构造方法Thread(Runnable target) 构造出对象,然后调用Thread对象的start()方法来运行多线程代码。

实际上所有的多线程代码都是通过运行Thread的start()方法来运行的。因此,不管是扩展Thread类还是实现Runnable接口来实现多线程,最终还是通过Thread的对象的API来控制线程的,熟悉Thread类的API是进行多线程编程的基础。

三、Thread和Runnable的区别

如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享


案列一:卖票

第一种: synchronized 
public class MyRunable implements Runnable {
	int ticket = 100;
	private Object obj = new Object();
	int x=0;
	@Override
	public void run() {
		while(true){
			if(x%2==0){
				synchronized(this){
					sellTickets();
				
				}
			}else{
				synchronized(this){
					sellTickets();
				
				}
			}
		}
	}
	
	public synchronized void sellTickets(){
		if(ticket>0){
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"正在出售第"+ticket--+"票");
		}
	}
}

public class MyRunableTest {
	public static void main(String[] args) {
		MyRunable my = new MyRunable();
		Thread thread1 = new Thread(my,"窗口1");
		Thread thread2 = new Thread(my,"窗口2"); 
		Thread thread3 = new Thread(my,"窗口3"); 
		thread1.start();
		thread2.start();
		thread3.start();
	}
}
第二种:lock
public class ThreadLock implements Runnable{
	int t = 100;
	private Lock lock = new ReentrantLock();//创建锁
	@Override
	public void run() {
		while(true){
			lock.lock();//上锁
			if(t>0){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+"正在出售第"+t--+"张票");
			}
			lock.unlock();//释放锁
		}
		
	}
	
}
public class lockTest {

	public static void main(String[] args) {
		ThreadLock tl = new ThreadLock();
		Thread t1 = new Thread(tl,"窗口1");
		Thread t2 = new Thread(tl,"窗口2");
		Thread t3 = new Thread(tl,"窗口3");
		
		t1.start();
		t2.start();
		t3.start();
	}
}



案列二:生产和消费

1.创建学生类
public class student {
	private String name;
	private int age;
	private boolean flag;
	
	public student() {
	}
	public student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public boolean isFlag() {
		return flag;
	}
	public void setFlag(boolean flag) {
		this.flag = flag;
	}
	
}
2.设置学生
public class SetStudent implements Runnable {
	private student s;
	int x = 0;
	public SetStudent() {
	}
	public SetStudent(student s) {
		this.s=s;
	}
	@Override
	public void run() {
		while(true){
			synchronized (s) {
				if(s.isFlag()){
					try {
						s.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if(x%2==0){
					s.setName("jjz");
					s.setAge(26);
				}else{
					s.setName("xjp");
					s.setAge(55);
				}
				x++;
				
				s.setFlag(true);
				s.notify();
			}
			
		}

	}

}
3.获取学生
package com.studentThread;

public class GetStudent implements Runnable {
	private student s;
	public GetStudent() {
		// TODO Auto-generated constructor stub
	}
	
	public GetStudent(student s) {
		super();
		this.s = s;
	}

	@Override
	public void run() {
		while(true){
			synchronized (s) {
				if(!s.isFlag()){
					try {
						s.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println(s.getName()+"---"+s.getAge());
				s.setFlag(false);
				s.notify();
			}
			
		}
	}

}

4.执行线程
public class StudentThread {

	public static void main(String[] args) {
		student s = new student();
		GetStudent gs = new GetStudent(s);
		SetStudent ss = new SetStudent(s);
		
		Thread t1= new Thread(gs);
		Thread t2= new Thread(ss);
//		t1.start();
//		t2.start();
		System.out.println(t1.getThreadGroup().getName());
		System.out.println(t2.getThreadGroup().getName());
		
	}
}


线程池:

public class MyCallable implements Callable<Integer> {
	private int start;
	private int end;
	
	public MyCallable(int start, int end) {
		this.start = start;
		this.end = end;
	}
	@Override
	public Integer call() throws Exception {
		int sum=0;
		for(int i=start;i<=end;i++){
			sum+=i;
		}
		return sum;
	}

}
public class poolDemo {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService pools = Executors.newFixedThreadPool(2);//创建线程池,有两个线程
		 Future<Integer> f1 = pools.submit(new MyCallable(1,2));
		 Future<Integer> f2 = pools.submit(new MyCallable(3,4));
		 
		 Integer i1= f1.get();
		 Integer i2= f2.get();
		 System.out.println(i1);
		 System.out.println(i2);
		
	}

}


定时器

public class myTimer {

	public static void main(String[] args) {
		Timer t = new Timer();
		t.schedule(new timerTest(t), 3000,1000);//每隔1秒钟执行定时器
	}
	
	
}
class timerTest extends TimerTask{
	private Timer t;
	
	public timerTest() {}
	
	public timerTest(Timer t) {
		super();
		this.t = t;
	}

	@Override
	public void run() {
		System.out.println("嘭!!爆破成功!");
		t.cancel();//停止执行
	}
	
}

定时删除指定文件夹下的文件

public class TimerFileDel {

	public static void main(String[] args) throws ParseException {
		String s = "2016-11-25 15:01:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date d = sdf.parse(s);
		
		Timer t = new Timer();
		t.schedule(new TimerTests(t), d);//指定时间删除文件
	}

}

class TimerTests extends TimerTask{
	private Timer t;
	public TimerTests() {
		// TODO Auto-generated constructor stub
	}
	
	public TimerTests(Timer t) {
		this.t = t;
	}

	@Override
	public void run() {
		String path = "D:"+File.separator+"API";
		File file = new File(path);
		delFile(file);
		t.cancel();
	}
	
	public void delFile(File file){
		if(file!=null){
			File[] f = file.listFiles();//得到文件夹下的所有文件
			for (File file2 : f) {//遍历数组
				if(file2.isDirectory()){//如果是文件夹
					delFile(file2);//递归
				}else{//不是的文件夹就删除
					System.out.println(file2.getName()+"--"+file2.delete());
				}
			}
			System.out.println(file.getName()+"---------------------"+file.delete());//删除文件夹
		}
		
	}
	 
}












 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值