1.加锁处理线程安全问题01
package 多线程;
import java.util.concurrent.locks.Lock;
public class 加锁解决线程安全问题 {
public static void main(String[] args) {
ThreadRunnable runnable = new ThreadRunnable();
Thread thread1 = new Thread(runnable,"售票点1");
Thread thread2 = new Thread(runnable,"售票点2");
Thread thread3 = new Thread(runnable,"售票点3");
Thread thread4 = new Thread(runnable,"售票点4");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
//用extends实现时 注意将锁设置成static 为共有的
class ThreadRunnable implements Runnable{
private int count = 100;
private Object lock = new Object();//可以使用任何引用对象来声明 没有其他作用
@Override
public void run() {
while(true){
synchronized (lock){//lock也可以为this
//第一个线程来了锁住了并拿到钥匙 当第二个线程来的时候只能等待 因为它拿不到钥匙
if (count!=0){
System.out.println(Thread.currentThread().getName()+"卖出第"+count+"票");
count--;
}else{
break;
}
}
try{
java.lang.Thread.sleep(10);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
2.加锁处理线程安全问题02
package 多线程;
public class 加锁解决线程安全2 {
public static void main(String[] args) {
多线程.ThreadRunnable runnable = new 多线程.ThreadRunnable();
Thread thread1 = new Thread(runnable,"售票点1");
Thread thread2 = new Thread(runnable,"售票点2");
Thread thread3 = new Thread(runnable,"售票点3");
Thread thread4 = new Thread(runnable,"售票点4");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
//用extends实现时 注意将锁设置成static 为共有的
class ThreadRunnable2 implements Runnable{
private int count = 100;
@Override
public void run() {
while(count>0){
//将可能出现线程安全的封装到一个方法中,然后调用此加锁的方法
sellTicket();
try{
java.lang.Thread.sleep(10);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
public synchronized void sellTicket(){
if (count>0){
System.out.println(Thread.currentThread().getName()+"卖出第"+count+"张票");
count--;
}
}
}
3.加锁处理线程安全问题03(锁对象)
package 多线程;
import java.util.concurrent.locks.ReentrantLock;
public class 加专门的锁对象 {
public static void main(String[] args) {
多线程.ThreadRunnable runnable = new 多线程.ThreadRunnable();
Thread thread1 = new Thread(runnable,"售票点1");
Thread thread2 = new Thread(runnable,"售票点2");
Thread thread3 = new Thread(runnable,"售票点3");
Thread thread4 = new Thread(runnable,"售票点4");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
//用extends实现时 注意将锁设置成static 为共有的
class ThreadRunnable3 implements Runnable{
private int count = 100;
private ReentrantLock reentrantLock = new ReentrantLock();
@Override
public void run() {
while(count>0){
reentrantLock.lock();//加锁、
try{//确保万一出现异常解锁失败
if (count>0){
System.out.println(Thread.currentThread().getName()+"卖出第"+count+"张票");
count--;
}
}catch(Exception e){
System.out.println(e.getMessage());
}
reentrantLock.unlock();//解锁
try{
java.lang.Thread.sleep(10);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
}
4.线程的中断
package 多线程;
/*
Thread.stop() 直接杀死 一枪崩掉
Thread.interrupt() 将状态设置成中断 然后自己处理关闭杀死 自己处理杀死自己 可以留下遗言
*/
public class 线程的中断 {
public static void main(String[] args) {
Threads a = new Threads();
a.start();
//a.stop();
try{
Thread.sleep(5);
}catch(Exception e){
System.out.println(e.getMessage());
}
a.interrupt();
}
}
class Threads extends Thread{
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
if (interrupted()){
System.out.println("释放资源");
break;
}
System.out.println("Thread"+i);
}
}
}
5.线程组
package 多线程;
//批量管理
public class 线程组 {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
//Thread t1 = new Thread(r,"线程1");
//Thread t2 = new Thread(r,"线程2");
//创建线程组
ThreadGroup tg = new ThreadGroup("我们的线程组");
Thread t1 = new Thread(tg,r);
Thread t2 = new Thread(tg,r);
System.out.println(t1.getThreadGroup().getName());
System.out.println(t2.getThreadGroup().getName());
tg.setDaemon(true);//设置守护线程
tg.setMaxPriority(9);//设置最大有限级别
/*ThreadGroup threadGroup1 = t1.getThreadGroup();
ThreadGroup threadGroup2 = t2.getThreadGroup();//获取线程组
System.out.println(threadGroup1.getName());//获取线程组名字
System.out.println(threadGroup2.getName());//*/
t1.start();
t2.start();
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
}
6.设置线程优先级
package 多线程;
public class 设置线程优先级 {
public static void main(String[] args) {
Thread mainThread = Thread.currentThread();
mainThread.setPriority(10);//设置为最大
Thread1 thread1 = new Thread1();
thread1.setPriority(1);// 置为最小
System.out.println(mainThread.getPriority());
System.out.println(thread1.getPriority());
for (int i = 0; i < 100; i++) {
System.out.println("mainThread"+i);
}
thread1.start();//启动那个线程
}
}
class Thread1 extends Thread{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("Thread1"+i);
}
}
}