Sleep
使得当前线程休眠。
Interrupt打断,万万不得以不要用Stop,Stop太粗暴了~~
import java.util.*;
public class TestInterrupt{
public static void main(String[] args){
MyThread thread=new MyThread();
thread.start();//开始一个线程
try{
Thread.sleep(10000);//线程睡10秒
}catch(InterruptedException e){}
thread.interrupt();//终止线程,并非最好的停止线程的做法
}
}
class MyThread extends Thread{
public void run(){
while(true){
System.out.println("==="+new Date() + "===");
try{
sleep(1000);
}catch(InterruptedException e){
return;
}
}
}
}
Join
调用某线程的该方法,将当前线程与该线程合并,即等待该线程合并,再恢复当前线程的运行。
public class TestJoin{
public static void main(String[] args){
MyThread2 t1=new MyThread2("abcde"); //实例化一个当前线程
t1.start(); //开启线程
try{
t1.join(); //合并线程
}catch(InterruptedException e){}
for(int i=1;i<=10;i++){
System.out.println("i am main thread");
}
}
}
class MyThread2 extends Thread{ //定义一个Thread子类,重写run方法,创建新的线程
MyThread2 (String s){
super(s);
}
public void run(){
for(int i =1; i<=10;i++){
System.out.println("i am" +getName());
try{
sleep(1000);
}catch(InterruptedException e){
return;
}
}
}
}
线程同步
public class TestSync implements Runnable{
Timer timer=new Timer();
public static void main(String[] args){
TestSync test=new TestSync();
Thread t1=new Thread(test);
Thread t2=new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run(){
timer.add(Thread.currentThread().getName());
}
}
class Timer{
private static int num=0;
public void add(String name){
synchronized (this){ //添加锁机制,互斥锁,或者public synchronized void add(String name){,锁定当前执行方法
num++;
try{Thread.sleep(1);}
catch(InterruptedException e){}
System.out.println(name+",你是第" + num + "使用timer的线程");
}
}
}
不加锁的时候,不能实现线程唯一。
加锁之后
死锁
public class TestDeadLock implements Runnable{
public int flag=1;
static Object o1=new Object(),
o2=new Object();
public void run(){
System.out.println("flag="+flag);
if (flag==1){
synchronized(o1){//添加互斥锁
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
synchronized(o2){
System.out.println("1");
}
}
}
if (flag==0){
synchronized(o2){
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
synchronized(o1){
System.out.println("o");
}
}
}
}
public static void main(String[] args){
TestDeadLock td1=new TestDeadLock();
TestDeadLock td2=new TestDeadLock();
td1.flag=1;
td2.flag=0;
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
t1.start();
t2.start();
}
}
面试题
public class TT implements Runnable{
int b=100;
public synchronized void m1() throws Exception{
b=1000;//只是锁定了当前的这段话,这段话不能被锁定
Thread.sleep(5000);
System.out.println("b=" +b);
}
public void m2(){
System.out.println(b);
}
public void run(){
try{
m1();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
TT tt=new TT();
Thread t=new Thread(tt);
t.start();
Thread.sleep(1000);
tt.m2();
}
}
输出结果:
如果要保护好某个类里面的某个对象,必须对访问的所有对象的方法仔细的考虑加不加同步,加了同步效率就会变低。加了同步,有可能产生数据不一致的现象。非加锁的方法还可以继续访问。
生产者消费者问题
public class ProducerConsumer{
public static void main(String[] args){
SyncStack ss=new SyncStack(); //放馒头的框
Producer p=new Producer(ss);//实例化生产者
Consumer c=new Consumer(ss);//实例化消费者
new Thread(p).start();//生产开始
new Thread(c).start();//消费开始
}
}
//窝头
class WoTou{
int id;
WoTou(int id){
this.id=id;
}
public String toString(){
return "wotou:" + id;
}
}
//放窝头的框
class SyncStack{
int index=0;
WoTou[] arrWT=new WoTou[6];
//放入窝头
public synchronized void push (WoTou wt){
while(index==arrWT.length){
try{
this.wait();//wait的时候,其他线程还能来访问锁内资源,sleep则不可用,wait必须要加锁
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notifyAll();//通知
arrWT[index]=wt; //传入的窝头放入框的数组中
index ++;//继续存放下一个窝头
}
//弹出窝头
public synchronized WoTou pop(){
while(index==0){
try{
this.wait();//如果框里面没有窝头了,要等一等
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.notifyAll();通知大家
index--;//下一个窝头
return arrWT[index];//返回下一个窝头的序号
}
}
class Producer implements Runnable{//通过实现Runnable接口来创建一个新的线程
SyncStack ss=null;
Producer(SyncStack ss){
this.ss=ss;
}
public void run(){
for(int i =0;i<20;i++){
WoTou wt=new WoTou(i);
System.out.println("生产了:" + wt);
ss.push(wt);
try{
Thread.sleep((int )(Math.random()*2));
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
SyncStack ss=null; //初始化框为空
Consumer(SyncStack ss){ //构造消费者
this.ss=ss;
}
//开始进行消费
public void run(){
for(int i =0;i<20;i++){
WoTou wt=ss.pop();
System.out.println("消费了:" + wt);
try{
Thread.sleep((int )(Math.random()*1000));
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}