一、sleep()方法、wait()方法、yeild()方法、interrupt()方法、notify()、notifyAll()方法
2、wait()方法: 当wait方法被调用时,它会释放它所占用的锁标记,从而使线程所在对象中的synchronize数据可以被别的线程所使用, 3、yeild()方法:
java线程的状态 java线程有四种状态:产生、就绪、执行、阻塞、死亡
阻塞: java实现多线程的方法 java实现多线程的方法:继承Thread类,实现runnable接口,实现Callable接口
public class MyThread extends Thread {
public void run() {
System.out.println('MyThread.run()');
}
}
MyThread myThread1 = new MyThread();
myThread1.start(); 2、实现Runnable接口: public class MyThread immplements Runnable {
public void run(){
sysout.out.println('MyThread.run()');
}
}
MyThread runThread = new MyThread();
Thread runThread = new Thread(runThread,'');
runThread.start(); 3、实现Callable接口,通过FutureTask包装器创建Thread线程 优缺点: 线程调度 1.调整现场优先级:Java线程有优先级,优先级高的线程获得较多的运行机会(运行时间);
5.线程加入:join()方法,在当前线程中调用另一个线程的join()方法,则当前线程转入阻塞状态,知道另一个进程运行结束,当前线程再有阻塞状态转为就绪状态; 线程类的一些常用方法 1)sleep():强迫一个线程睡眠N毫秒; 线程同步 线程同步主要使用synchronized关键字;具体有两种实现方式:1、作为关键字修饰类中一个方法;2、修饰方法中的一块区域(代码); public class Name{//类名为Name
//getName方法
public synchronized void getName(){
system.out.println(“123”);
} } 类Name 有两个实例对象n1和n2,此时有两个线程t1和t2;n1在线程t1中执行getName()方法(获得这个方法的锁),那么n1就不能在线程t2中执行getName()方法了,但是n2可以在t1线程中执行getName()方法,同理n2 不能同时在t2线程中执行getName()方法; 所以说实际上synchronized锁的是getName()这个方法的对象(n1和n2),而不是锁的这个方法,这个需要理解;
public void getName(Object o){
synchronized(o){
//TODO
}
} 这里表示锁住这个变量o; public class Car{
//构造方法私有化
private Car();
//创建一个静态的私有的空的常量car
private static Car car = null;
//对外开放一个静态的共有的方法用来获取实例
public static getInstance(){
if(car == null){
synchronized(Car.getClass()){
if(car == null){
car = new Car();
}
}
}
return car;
} } 线程数据传递 在传统的开发模式下,当我们调用一个函数时,通过这个函数的参数将数据传入,并通过这个函数的返回值来返回最终的计算结果,但是在多线程的异步开发模式下,数据的传递和返回同同步开发模式有很大区别。 由于线程的运行和结果是不可预料的,因此在传递和返回数据时就无法像函数一样通过函数参数和return语句来返回数据; 1、通过构造方法传递参数 package mythread; public class MyThread1 extends Thread { private String name; public MyThread1(String name) { this.name = name; } public void run() { System.out.println('hello ' name); } public static void main(String[] args) { Thread thread = new MyThread1('world'); thread.start(); } } 由于这种方法是在创建线程对象的同时传递数据的,因此,在线程运行之前这些数据就就已经到位了,这样就不会造成数据在线程运行后才传入的现象。 如果要传递更复杂的数据,可以使用集合、类等数据结构。使用构造方法来传递数据虽然比较安全,但如果要传递的数据比较多时,就会造成很多不便。 由于Java没有默认参数,要想实现类似默认参数的效果,就得使用重载,这样不但使构造方法本身过于复杂,又会使构造方法在数量上大增。因此,要想避免这种情况,就得通过类方法或类变量来传递数据。
2、通过变量和方法传递数据 然后在建立完对象后,通过对象实例逐个赋值。下面的代码是对MyThread1类的改版,使用了一个setName方法来设置 name变量: package mythread; public class MyThread2 implements Runnable { private String name; public void setName(String name) { this.name = name; } public void run() { System.out.println('hello ' name); } public static void main(String[] args) { MyThread2 myThread = new MyThread2(); myThread.setName('world'); Thread thread = new Thread(myThread); thread.start(); } } 3、最后是线程之间的数据共享,以下贴出生产者消费者模式的实现:
package com.yp.producerAndconsumer;/** * @prama 生产者消费者问题中的产品 */public class Mantou {
private String mt;
private int num;
public String getMt() {
return mt;
}
public void setMt(String mt) {
this.mt = mt;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public Mantou(String mt, int num) {
this.mt = mt;
this.num = num;
}
/** * 消费馒头 */
public synchronized void eatMantou(){
//如果馒头数量大于0,则消费馒头
while(num >0){
System.out.println('消费后有:---' this.num '\n');
num--;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while(num ==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.notify();
}
}
/** * 生产馒头 */
public synchronized void produceMantou(){
while(num<5){
System.out.println('生产后有:---' this.num '个' '\n');
num ;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while(num ==5){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
this.notify();
}
} } 消费者类; package com.yp.producerAndconsumer; public class Consumer implements Runnable { private Mantou m ; public Consumer(Mantou m) { this.m = m; } @Override public void run() { m.eatMantou(); } } 生产者类: package com.yp.producerAndconsumer; public class Producer implements Runnable { Mantou m ; public Producer(Mantou m) { this.m = m; } @Override public void run() { m.produceMantou(); } } 测试类: package com.yp.producerAndconsumer; /** * @param 生产者消费者问题 * @author YangPeng * */ public class ProducerAndConsumer { public static void main(String[] args) { Mantou mantou = new Mantou('花卷',4); Producer producer = new Producer(mantou); Consumer consumer = new Consumer(mantou); Thread t1 = new Thread(producer); Thread t2 = new Thread(consumer); t1.start(); t2.start(); } } |
|