JAVA学习日记-多线程-代码PART2

ThreadTest11.java:

//线程的合并
public class ThreadTest11 {
	public static void main(String[] args)throws Exception{
		Thread t = new Thread(new Processor9());
		
		t.setName("t");
		
		t.start();
		
		//合并线程
		t.join();//t和主线程合并,单线程的程序
		
		//主线程的程序
		for(int i=0;i<10;i++){
			System.out.println(Thread.currentThread().getName()+"--->"+i);
		}
		
	}

}
class Processor9 implements Runnable{
	public void run(){
		for(int i=0;i<5;i++){
			try{
				Thread.sleep(1000);
			}catch(Exception e){
				
			}
			System.out.println(Thread.currentThread().getName()+"--->"+i);
		}
	}
}

ThreadTest12.java:

/*
t1和t2

异步编程模型:t1线程执行t1的,t2线程执行t2的,两个线程之间谁也不等谁。

同步编程模型:t1线程和t2线程执行,当t1线程必须等t2线程执行结束之后,t1线程才能执行,这是同步编程模型。


什么时候要同步呢?为什么要引入线程同步呢?
	1.为了数据的安全。尽管应用程序的使用率降低,但是为了保证数据是安全的,必须加入线程同步机制。
	线程同步机制使程序变成了(等同)单线程。

	2.什么条件下要使用线程同步?
		第一:必须是多线程环境
		第二:多线程环境共享同一个数据.
		第三:共享的数据涉及到修改操作。

以下程序演示取款例子。以下程序不使用线程同步机制,多线程
同时对同一个账户进行取款操作,会出现什么问题?
*/
public class ThreadTest12 {
	public static void main(String[] args){
		
		//创建一个公共的账户
		Account act = new Account("actno-001",5000.0);
		
		//创建线程对同一账户进行取款
		Thread t1 = new Thread(new Processor10(act));
		Thread t2 = new Thread(new Processor10(act));
		
		t1.start();
		t2.start();
		
	}

}
//取款线程
class Processor10 implements Runnable{
	//账户
	Account act;
	
	//Constructor
	Processor10(Account act){
		this.act = act;
	}
	public void run(){
		act.withdraw(1000);
		System.out.println("取款1000.0成功,余额为"+act.getBalance());
	}
}
//账户
class Account{
	private String actno;
	private double balance;
	
	public Account(){}
	public Account(String actno,double balance){
		this.actno = actno;
		this.balance = balance;
	}
	//setter getter
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno = actno;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double after) {
		this.balance = after;
	}
	//对外提供一个取款的方法
	public void withdraw(double money){//对当前账户进行取款操作
		
		double after = balance - money;
		
		//延迟
		try{Thread.sleep(1000);}catch(Exception e){}
		
		//更新
		this.setBalance(after);
	}
}

ThreadTest13.java:

/*
以下程序使用线程同步机制保证数据的安全。
*/
public class ThreadTest13 {
	public static void main(String[] args){
		
		//创建一个公共的对象
		Account13 act = new Account13("actno-001",5000.0);
		
		System.out.println("当前账户余额:"+act.getBalance());
		
		
		//创建线程对同一账户进行取款
		Thread t1 = new Thread(new Processor13(act));
		Thread t2 = new Thread(new Processor13(act));
		
		t1.start();
	
		t2.start();

	
	}

}
//取款线程
class Processor13 implements Runnable{
	//账户
	Account13 act;
	
	//Constuctor
	Processor13(Account13 act){
		this.act = act;
	}
	public void run(){
		act.withdraw(1000.0);
			System.out.println("成功取款1000.0,余额:"+act.getBalance());
		}
}


//账户
class Account13{
	private String actno;
	private double balance;
	
	public Account13(){}
	public Account13(String actno,double balance){
		this.actno = actno;
		this.balance = balance;
	}
	//setter and getter
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno = actno;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	//对外提供一个取款方法
	public void withdraw(double money){//对当前账户进行取款操作
		//把需要同步的代码,放到同步语句块中.
		/*
		原理:t1线程和t2线程.
		t1线程执行到此处,遇到了synchronized关键字,就会去找this的对象锁,
		如果找到this对象锁,则进入同步语句块中执行程序。当同步语句块中的代码
		执行结束之后,t1线程归还this的对象锁。

		在t1线程执行同步语句块的过程中,如果t2线程也过来执行以下代码,也遇到
		synchronized关键字,所以也去找this的对象锁,但是该对象锁被t1线程持有,
		只能在这等待this对象的归还。
		*/
		synchronized(this){
			
			double after = balance - money;
			
			//延迟
			try{Thread.sleep(1000);}catch(Exception e){}
			
			//更新
			this.setBalance(after);
		}
	}
	
}

ThreadTest14.java:

/*
以下程序使用线程同步机制保证数据的安全。
*/
public class ThreadTest14 {
	public void main(String[] args){

		//创建一个公共的对象
		Account14 act = new Account14("actno-001",5000.0);
		
		System.out.println("当前账户余额:"+act.getBalance());
		
		
		//创建线程对同一账户进行取款
		Thread t1 = new Thread(new Processor14(act));
		Thread t2 = new Thread(new Processor14(act));
		
		t1.start();
	
		t2.start();

	
	}

}
//取款线程
class Processor14 implements Runnable{
	//账户
	Account14 act;
	
	//Constuctor
	Processor14(Account14 act){
		this.act = act;
	}
	public void run(){
		act.withdraw(1000.0);
			System.out.println("成功取款1000.0,余额:"+act.getBalance());
		}
}


//账户
class Account14{
	private String actno;
	private double balance;
	
	public Account14(){}
	public Account14(String actno,double balance){
		this.actno = actno;
		this.balance = balance;
	}
	//setter and getter
	public String getActno() {
		return actno;
	}
	public void setActno(String actno) {
		this.actno = actno;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	//对外提供一个取款方法
	
	//synchronized关键字添加到成员方法上,线程拿走的也是this的对象锁
	public synchronized void withdraw(double money){//对当前账户进行取款操作
		//把需要同步的代码,放到同步语句块中.
		/*
		原理:t1线程和t2线程.
		t1线程执行到此处,遇到了synchronized关键字,就会去找this的对象锁,
		如果找到this对象锁,则进入同步语句块中执行程序。当同步语句块中的代码
		执行结束之后,t1线程归还this的对象锁。

		在t1线程执行同步语句块的过程中,如果t2线程也过来执行以下代码,也遇到
		synchronized关键字,所以也去找this的对象锁,但是该对象锁被t1线程持有,
		只能在这等待this对象的归还。
		*/
		
			
			double after = balance - money;
			
			//延迟
			try{Thread.sleep(1000);}catch(Exception e){}
			
			//更新
			this.setBalance(after);
		

	}

}

ThreadTest15.java:

//面试题
public class ThreadTest15 {
	public static void main(String[] args)throws Exception{
		
		MyClass mc = new MyClass();
		
	    Processor15 p = new Processor15(mc);
		
		Thread t1 = new Thread(p);
		t1.setName("t1");
		Thread t2 = new Thread(p);
		t2.setName("t2");
		
		//启动线程
		t1.start();
		
		//延迟(保证t1先启动,并执行run
		Thread.sleep(1000);
		
		t2.start();
	}

}
class Processor15 implements Runnable{
	MyClass mc ;
	Processor15(MyClass mc){
		this.mc = mc;
	}
	public void run(){
		if(Thread.currentThread().getName().equals("t1")){
			mc.m1();
		}
		if(Thread.currentThread().getName().equals("t2")){
			mc.m2();
		}
	}

}
class MyClass{
	public synchronized void m1(){
		//休眠
		try{Thread.sleep(10000);}catch(Exception e){}
		System.out.println("m1...");
	}
		/*
		//m2方法的执行不需要等m1的结束,因为m2方法上没有synchronized
		public void m2(){
			System.out.println("m2....");
		}
		*/
	//m2方法会等m1方法结束,t1,t2共享同一个mc,并且m1和m2方法上都有synchronized
		public synchronized void m2(){
			System.out.println("m2...");
	}
	
}

ThreadTest16.java:

//面试题
public class ThreadTest16 {
	
	public static void main(String[] args)throws Exception{
	
	MyClass16 mc1 = new MyClass16();
	MyClass16 mc2 = new MyClass16();
	
    Processor16 p1 = new Processor16(mc1);
    Processor16 p2 = new Processor16(mc2);
	
	Thread t1 = new Thread(p1);
	t1.setName("t1");
	Thread t2 = new Thread(p2);
	t2.setName("t2");
	
	//启动线程
	t1.start();
	
	//延迟(保证t1先启动,并执行run
	Thread.sleep(1000);
	
	t2.start();
}

}
class Processor16 implements Runnable{
MyClass16 mc ;
Processor16(MyClass16 mc){
	this.mc = mc;
}
public void run(){
	if(Thread.currentThread().getName().equals("t1")){
		mc.m1();
	}
	if(Thread.currentThread().getName().equals("t2")){
		mc.m2();
	}
}

}
class MyClass16{
public synchronized void m1(){
	//休眠
	try{Thread.sleep(10000);}catch(Exception e){}
	System.out.println("m1...");
}


    //m2方法不会等m1方法结束 t1 t2不共享一个mc
	public synchronized void m2(){
		System.out.println("m2...");
	}

}

ThreadTest17.java:

//类锁 类只有一个,所以锁是级别类的,只有一个
public class ThreadTest17 {
	public static void main(String[] args)throws Exception{
		Thread t1 = new Thread(new Processor17());
		Thread t2 = new Thread(new Processor17());
		
		t1.setName("t1");
		t2.setName("t2");
		
		t1.start();
		
		//延迟  保证t1先启动
		Thread.sleep(1000);
		
		t2.start();
	}

}
class Processor17 implements Runnable{
	public void run(){
		if(Thread.currentThread().getName().equals("t1")){
			MyClass17.m1();
		}
		if(Thread.currentThread().getName().equals("t2")){
			MyClass17.m2();
		}
	}
	
}
class MyClass17{
	//synchronized添加到静态方法上,线程执行此方法会寻找类锁
	public synchronized static void m1(){
		try{Thread.sleep(10000);}catch(Exception e){}
		System.out.println("m1...");
		//不会等m1结束,因为该方法没有被synchronized修饰
		/*
		public static void m2(){
			System.out.println("m2...");
		}
		*/

		//m2方法等m1结束之后才能执行,该方法有synchronized
		//线程执行该代码需要“类锁”,而类锁只有一个。
	}
		public synchronized static void m2(){
			System.out.println("m2...");
		}
	}


ThreadTest18.java:

//类锁 类只有一个,所以锁是级别类的,只有一个
public class ThreadTest18 {
	public static void main(String[] args)throws Exception{
		MyClass18 mc1 = new MyClass18();
		MyClass18 mc2 = new MyClass18();
		
		
		Thread t1 = new Thread(new Processor18(mc1));
		Thread t2 = new Thread(new Processor18(mc2));	
		t1.setName("t1");
		t2.setName("t2");
		
		t1.start();
		
		//延迟  保证t1先启动
		Thread.sleep(1000);
		
		t2.start();
	}

}
class Processor18 implements Runnable{
	MyClass18 mc;
	Processor18(MyClass18 mc){
		this.mc = mc;
	}
	public void run(){
		if(Thread.currentThread().getName().equals("t1")){
			mc.m1();
		}
		if(Thread.currentThread().getName().equals("t2")){
			mc.m2();
		}
	}
	
}
class MyClass18{
	//synchronized添加到静态方法上,线程执行此方法会寻找类锁
	public synchronized static void m1(){
		try{Thread.sleep(10000);}catch(Exception e){}
		System.out.println("m1...");
	

		//m2方法等m1结束之后才能执行,该方法有synchronized
		//线程执行该代码需要“类锁”,而类锁只有一个。
	}
		public synchronized static void m2(){
			System.out.println("m2...");
		}
	}




ThreadTest19.java:

/*
守护线程.

其他所有的用户线程结束,则守护线程退出!
守护线程一般都是无限执行的.
*/
public class ThreadTest19 {
	public static void main(String[] args) throws Exception{
		Thread t1 = new Processor19();
		t1.setName("t1");
		
		//将t1这个用户线程修改为守护线程
		t1.setDaemon(true);
		
		t1.start();
		
		//主线程
		for(int i=0;i<10;i++){
			System.out.println(Thread.currentThread().getName()+"--->"+i);
			Thread.sleep(1000);
		}
		
	}

}
class Processor19 extends Thread{
	public void run(){
		int i = 0;
		while(true){
			i++;
			System.out.println(Thread.currentThread().getName()+"--->"+i);
			try{Thread.sleep(500);}catch(Exception e){}
		}
	}
}

DeadLock.java:

//死锁
public class DeadLock {
	public static void main(String[] args){
		Object o1 = new Object();
		Object o2 = new Object();
		
		Thread t1 = new Thread(new T1(o1,o2));
		Thread t2 = new Thread(new T2(o1,o2));
		
		t1.start();
		t2.start();
	}

}
class T1 implements Runnable{
	Object o1;
	Object o2;
	
	T1(Object o1,Object o2){
		this.o1 = o1;
		this.o2 = o2;
	}
	public void run(){
		synchronized(o1){
			try{Thread.sleep(1000);}catch(Exception e){}
			synchronized(o2){
				
			}
		}
	}
}
class T2 implements Runnable{
	Object o1;
	Object o2;
	T2(Object o1,Object o2){
		this.o1 = o1;
		this.o2 = o2;
	}
	public void run(){
		synchronized(o2){
			try{Thread.sleep(1000);}catch(Exception e){}
			synchronized(o1){
		}
	}
}
}
发布了20 篇原创文章 · 获赞 0 · 访问量 262

猜你喜欢

转载自blog.csdn.net/weixin_43150344/article/details/104033007
今日推荐