Java多线程

多线程实现的两种方式

package com.a.b;
//多线程实现 继承thread类,成为线程主类
class MyThread extends Thread {
	private String title;
	
	public MyThread(String title) {
		this.title = title;
	} 
	
	//通过覆写Thread类中的run方法来实现线程启动的主方法
	@Override
	public void run() {
		for (int x = 0; x < 30; x++) {
			System.out.println(this.title  + "->" + x);
		}
	}
}

public class TestDemoB {
	public static void main(String[] args) {
		MyThread mtA = new MyThread("线程A");
		MyThread mtB = new MyThread("线程B");
		MyThread mtC = new MyThread("线程C");
		mtA.start();//start方法是多线程启动的唯一方法,线程都是彼此交替执行的
		mtB.start();//由于thread类中的start方法是native定义的,所以他是JVM调用的
		mtC.start();//其不仅启动多线程,还要进行不同操作系统的资源调配
	}

}
package com.a.c;



//多线程实现通过实现Runnable接口
class MyThread implements Runnable {
	String title;
	
	public MyThread(String title) {
		this.title = title;
	}
	@Override
	public void run() {
		for (int x = 0; x < 30; x++) {
			System.out.println(this.title + "->" + x);
		}
	}
}

public class TestDemoC {

	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程a");
		MyThread mt2 = new MyThread("线程b");
		MyThread mt3 = new MyThread("线程c");
		//thread中有一个有参构造函数public Thread(Runnable target)
		//可以接收Runnable接口对象
		new Thread(mt1).start();
		new Thread(mt2).start();
		new Thread(mt3).start();
	}

}

多线程两种实现方式的区别

​
package com.a.d;

//多线程两种实现方式的区别
class MyThread extends Thread {
	private int ticket = 5;
	@Override
	public void run() {
		for (int x = 0; x < 30; x++) {
			if ((this.ticket) > 0) {
				System.out.println("买票 ticket=" + this.ticket--);
			}
		}
	}
	
}

public class TestDemoD {

	public static void main(String[] args) {
		MyThread mt1 = new MyThread();
		MyThread mt2 = new MyThread();
		MyThread mt3 = new MyThread();
		mt1.start();
		mt2.start();
		mt3.start();//执行结果每一个线程对象各卖各的,不能进行数据共享
	}

}

​
package com.a.e;
//实现Runnable接口可以实现数据共享
class MyThread implements Runnable {
	private int ticket = 5;
	@Override
	public void run() {
		for (int x = 0; x < 30; x++) {
			if ((this.ticket) > 0) {
				System.out.println("买票ticket=" + this.ticket--);
			}
		}
	}
}

public class TestDemoE {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		//3个线程对象都接收同一个Runnable对象引用,实现数据共享
		new Thread(mt).start();
		new Thread(mt).start();
		new Thread(mt).start();

	}

}

线程的取得与命名

package com.a.f;
//线程的取得与命名
class MyThread implements Runnable {
	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName());
	}
}
public class TestDemoF {

	public static void main(String[] args) {
		//Thread类中public Thread(Runnable task,String name)
		MyThread mt = new MyThread();
		new Thread(mt,"线程A").start();
		new Thread(mt).start();
		new Thread(mt,"线程B").start();
		new Thread(mt).start();
		mt.run();

	}

}

线程的休眠

package com.a.g;
//线程的休眠
class MyThread implements Runnable {
	public void run() {
		for (int x =0; x < 10; x++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " x= " +x);
		}
	}
}

public class TestDemoG {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		//MyThread mtA = new MyThread();
		new Thread(mt,"线程A").start();
		new Thread(mt,"线程B").start();
	}

}

线程的优先级

package com.a.h;

//线程的优先级
class MyThread implements Runnable {
	@Override
	public void run() {
		for (int x = 0; x < 10; x++) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + " x = " + x);
		}
		
	}
}

public class TestDemoH {

	public static void main(String[] args) {
		MyThread mt = new MyThread();
		Thread t1 = new Thread(mt,"线程A");
		Thread t2 = new Thread(mt,"线程B");
		Thread t3 = new Thread(mt,"线程C");
		t3.setPriority(Thread.MAX_PRIORITY);
		t1.start();
		t2.start();
		t3.start();
		System.out.println(Thread.currentThread().getPriority());
	}

}

 生产者消费者的两个例子

package com.a.i;
//生产者消费者模式,生产者生产一个,消费者取一个
//解决数据完整性错位和重复取出与重复设置问题
//生产两条信息
//1 北南,Android
//2 南北,java
//解决数据错位问题让设置数据和取得数据进行同步操作,并把他们放到一个数据类中,用于存储和取出数据
//解决数据重复运用进程等待和唤醒机制,设置一个标志位boolean,ture表示可以生产,不能取
//false表示可以取出不能生产
class Message {
	private String title;
	private String content;
	private boolean flag = true;//默认表示可以生产,不能取

	
	public synchronized void set(String title, String content) {
		if (this.flag == false) {
			//判断数据的状态是否已经生产完,只能取
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}//让进程等一会
		}
		this.title = title;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.content = content;//用同步方法同步设置数据标题与内容
		this.flag = false;//表示正常设置完,只能取,不能设置了
		super.notify();//唤醒等待的线程,可以取了
	}
	
	public synchronized void get() {
		if (this.flag == true) {
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(this.title + "--" + this.content);
		this.flag = true;//表示正常取完,不能再取了,可以设置
		super.notify();//唤醒进程,可以设置了
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	
}

class Producer implements Runnable {
	private Message msg;
	public Producer(Message msg) {
		this.msg = msg;
	}
	@Override
	public void run() {
		for (int x = 0; x < 20; x++) {
			if ((x%2) == 0) {
				this.msg.set("北南","Android");
			} else {
				this.msg.set("南北", "Kotlin");
			}
		}
	}
}

class Consumer implements Runnable {
	private Message msg;
	public Consumer(Message msg) {
		this.msg = msg;
	}
	@Override
	public void run() {
		for (int x = 0; x < 20; x++) {
			this.msg.get();
		}
	}
}
public class TestDemoI {

	public static void main(String[] args) {
		Message msg = new Message();
		new Thread(new Producer(msg)).start();
		new Thread(new Consumer(msg)).start();

	}

}
package com.a.j;



class Message {
	private int data = 10 ;						// 初始值
	private boolean flag = true;
	public synchronized void add() {			// 加法操作
		if (this.flag == false) { 				// 已经生产过了,不能生产
			try {
				super.wait();					// 等待
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("加法操作:" + this.data ++);
		this.flag = false;						// 已经生产完成,修改标志位
		super.notify();							// 唤醒等待线程
	}
	public synchronized void subtract() {		// 减法操作
		if (this.flag == true) { 				// 未生产,不能取走
			try {
				super.wait();					// 等待
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("减法操作:" + this.data --);
		this.flag = true; 						// 已经取走了,可以继续生产
		super.notify();							// 唤醒等待线程
	}
	// setter、getter略
}
class Addition implements Runnable {				// 定义生产者
	private Message msg = null ;
	public Addition(Message msg) {
		this.msg = msg ;
	}
	@Override
	public void run() {
		for (int x = 0; x < 50; x++) {				// 加法执行50次
			this.msg.add() ;						// 加法操作
		}
	}
}
class Subtraction implements Runnable {				// 定义消费者
	private Message msg = null ;
	public Subtraction (Message msg) {
		this.msg = msg ;
	}
	@Override
	public void run() {
		for (int x = 0; x < 50; x++) {				// 减法执行50次
			this.msg.subtract() ;					// 执行减法
		}
	}
}
public class TestDemoJ {
	public static void main(String args[]) {
		Message msg = new Message(); 
		new Thread(new Addition(msg), "加法对象A").start(); 		// 启动线程
		new Thread(new Addition(msg), "家访对象B").start(); 		// 启动者线程
		new Thread(new Subtraction(msg), "减法对象A").start(); 	// 取得线程
		new Thread(new Subtraction(msg), "减法对象B").start(); 	// 取得线程
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值