JAVA基础-线程基础

简单Thread案例

线程优先级 常量:
-MAX_PRIORITY:线程的最高优先级10
-MIN_PRIORITY:线程的最低优先级1
-NORM_PRIORITY:线程的默认优先级5(跟主线程一样)
方法
public int getPriority() 获取线程优先级的方法
public void setPriority(int newPriority) 设置线程优先级的方法

package com.test.thread;
class MyThread extends Thread{
	public MyThread(String name) {
		super(name);
	}
	public void run() {
		for(int i=1;i<=4;i++) {
			System.out.println(getName()+"正在运行"+i);
		}
	}
}
public class ThreadTest {
	public static void main(String[] args) {
		MyThread mt1 = new MyThread("线程1");
		MyThread mt2 = new MyThread("线程2");
		mt1.start();
		//调用join方法先运行mt1线程在运行mt2线程
		try {
			mt1.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mt2.setPriority(10);//设置最高优先级 系统环境原因不一定最高优先级先运行
		//等价 mt2.setPriority(Thread.MAX_PRIORITY)
		mt2.start();	
		//获取主线程的优先级及设置 
		int mainPriority=Thread.currentThread().getPriority();
		System.out.println("这是主线程的输出,优先级为:"+mainPriority);
	}
}

简单runnable案例

package com.test.runnable;
class PrintRunnable implements Runnable{
	int i=1;
	@Override
	public void run() {
		while(i<10) {
		System.out.println(Thread.currentThread().getName()+"正在运行!"+(i++));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
}
public class Test {
	public static void main(String[] args) {
		PrintRunnable pr = new PrintRunnable();
		Thread t1 = new Thread(pr);
		t1.start();
		PrintRunnable pr2 = new PrintRunnable();
		Thread t2 = new Thread(pr);
		t2.start();
	}
}

线程通信案例: 生产者Producer 消费者Consumer

关键字synchronized 锁定不允许其他线程打断
可以用在 -成员方法 -静态方法 -语句块
public synchronized void saveAccount(){}
public static synchronize void saveAccount(){}
synchronized(obj){…}

wait()方法: 中断方法的执行,使线程等待
notify()方法: 唤醒处于等待的某一个线程,使其结束等待
notifyAll()方法: 唤醒所有处于等待的线程,使它们结束等待

项目包含4个文件-Consumer.java -Producer.java -Queue.java -Test.java

Test.java

package com.test.queue;
public class Test {
	public static void main(String[] args) {
		Queue queue = new Queue();
		new Thread(new Producer(queue)).start();
		new Thread(new Consumer(queue)).start();
	}
}

Queue.java

package com.test.queue;

public class Queue {
	private int n;
	boolean flag = false;
	//flag=false的时候表示Queue容器没有数据
	//这时候就要调用set方法先去生产数据
	public synchronized int getN() {
		if(!flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("消费:"+n);
		flag=false;//消费完毕,容器中没有数据
		notifyAll();
		return n;
	}

	public synchronized void setN(int n) {
		if(flag) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("生产:"+n);
		flag=true;//消费完毕,容器中没有数据
		notifyAll();
		this.n = n;
	}
}

Producer.java

package com.test.queue;

public class Producer implements Runnable {
	Queue queue;
	Producer(Queue queue){
		this.queue=queue;
	}
	@Override
	public void run() {
		int i=0;
		while (true) {
			queue.setN(i++);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}

Consumer.java

package com.test.queue;

public class Consumer implements Runnable {
	Queue queue;
	Consumer(Queue queue){
		this.queue=queue;
	}
	@Override
	public void run() {
		while(true) {
			queue.getN();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值