**thead.sleep() // 让其回到队列中,继续等待、
让线程休眠: super.wait(); 给一个条件,即可进行判断,让线程停止。
super.notify(); 睡眠后 需要手动唤醒,用这个关键字。 **
应用在如下代码中
public class Xianc2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Object o =new Object();
XC1 xc1 = new XC1();
XC1 xc2 = new XC1();
xc1.start();
xc2.start();
}
}
class XC1 extends Thread{
static Object o;
static int a=0;
public void XC1(Object o) {
this.o=o;
}
public void run() {
// TODO Auto-generated method stub
add();
}
public synchronized void add() { //为其上一个带锁的add方法。
// TODO Auto-generated method stub
for (int i = 0; a< 1000; i++) {
System.out.println(this.getName()+"\t"+a++);
if(a>500){
try {
super.wait(); //让线程休眠 当a>500时,线程停止
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
}
public class Xianc2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
XC1 xc1 = new XC1();
XC2 xc2 = new XC2(xc1);
XC3 xc3 = new XC3(xc1);
xc2.start();
xc3.start();
}
}
// sleep 到时间后,会自动进入队列
// wait 不会自动回到数列,需要控制相同对象资源 激活它
class XC1{
static int a=0;
static boolean boo=false;
public synchronized void add() {
// TODO Auto-generated method stub
System.out.println(a++);
if(a%5==0){
try {
System.out.println("add()wait");
boo=true;
super.wait();
System.out.println("aaa()被notify");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
public synchronized void add2() { //为其上一个带锁的add方法。
// TODO Auto-generated method stub
if(boo){
try {
System.out.println("add2唤醒");
super.notify();
boo=false;
} catch (Exception e) {
// TODO: handle exception
}
}
else
try {
Thread.sleep(200);
} catch (Exception e) {
// TODO: handle exception
}
}
}
class XC2 extends Thread{
XC1 xc1;
public XC2(XC1 xc1) {
this.xc1=xc1;
}
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
xc1.add();
}
}
}
class XC3 extends Thread{
XC1 xc1;
public XC3(XC1 xc1) {
this.xc1=xc1;
}
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
xc1.add2();
}
}
}
第三种,定义一个类变量的,没用父类(应该是这样)
public class Xianc1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
XC_1 xc1 = new XC_1();
XC_2 xc2 = new XC_2(xc1);
XC_3 xc3 = new XC_3(xc1);
xc2.start();
xc3.start();
}
}
class XC_1{
static Object o=new Object();
static int a=0;
static boolean boo=false;
public void wait_method() {
// TODO Auto-generated method stub
System.out.println(a++);
synchronized (o) {
if(a%5==0){
try {
System.out.println("wait_method()wait");
boo=true;
Thread.sleep(200);
o.wait();
System.out.println("wait_method()被notify");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
public boolean notify_method() {
// TODO Auto-generated method stub
synchronized (o) {
if(boo){
if(a%5==0){
try {
System.out.println("notiify_method()wait");
o.notify();
boo=false;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}return a<1000;
}
}
class XC_2 extends Thread{
XC_1 xc1;
public XC_2(XC_1 xc1) {
this.xc1=xc1;
}
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 1000; i++) {
xc1.wait_method();
}
}
}
class XC_3 extends Thread{
XC_1 xc1;
public XC_3(XC_1 xc1) {
this.xc1=xc1;
}
public void run() {
// TODO Auto-generated method stub
while(xc1.notify_method()){}
}
}
守护线程 垃圾回收,守护性能,例如写个日志啥的 对象名.setDaemon(true)
只要main没停止,守护线程就可以执行下去,main要是执行结束,守护线程就结束,
public class SHxc {
public static void main(String[] args) {
// TODO Auto-generated method stub
Shou s=new Shou();
s.start();
for (int i = 0; i < 1000; i++) {
System.out.println("main"+i);
try {
Thread.sleep(5);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}
class Shou extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
int a=1;
while(true){
System.out.println("\t\t\t"+a++);
try {
Thread.sleep(10);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}