标签:demo notifyall resource ble except 恢复 rgs 结束 实现
摘要:JAVA thread
class Demo extends Thread{
public void run(){
for(int x = 0; x < 60; x++){
System.out.println("Demo Run " + x);
}
}
}
class Test{
public static void main(String args[]){
Demo mDemo = new Demo();
mDemo.start();
for(int x = 0; x < 60; x++){
System.out.println("Main Function " + x);
}
}
}
class Hello{
public static void main(String[] args){
Resource r = new Resource();
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
}
}
class Resource{
String name;
int sex;
}
class Input implements Runnable{
Resource r;
Input(Resource r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
if(x == 0){
r.name = "A";
r.sex = 0;
}else{
r.name = "B";
r.sex = 1;
}
x = (x + 1)%2;
}
}
}
class Output implements Runnable{
Resource r = new Resource();
Output(Resource r){
this.r = r;
}
public void run(){
while(true){
System.out.println(r.name + "..." + r.sex);
//B...0, B...1, A...0, A...1 问题发生
}
}
}
解决 加入同步
class Input implements Runnable{
Resource r;
Input(Resource r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized(r){
if(x == 0){
r.name = "A";
r.sex = 0;
}else{
r.name = "B";
r.sex = 1;
}
x = (x + 1)%2;
}
}
}
}
class Output implements Runnable{
Resource r;
Output(Resource r){
this.r = r;
}
public void run(){
while(true){
synchronized (r) {
System.out.println(r.name + "..." + r.sex);
}
}
}
}
synchronized(对象)
class Resource{
String name;
int sex;
boolean flag = false;
}
class Input implements Runnable{
Resource r;
Input(Resource r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
synchronized(r){
if(r.flag)
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(x == 0){
r.name = "A";
r.sex = 0;
}else{
r.name = "B";
r.sex = 1;
}
x = (x + 1)%2;
r.flag = true;
r.notify();
}
}
}
}
class Output implements Runnable{
Resource r;
Output(Resource r){
this.r = r;
}
public void run(){
while(true){
synchronized (r) {
if(!r.flag){
try {
r.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(r.name + "..." + r.sex);
r.flag = false;
r.notify();
}
}
}
}
wait(), notity(), notifyAll()
class Hello{
public static void main(String[] args){
Resource r = new Resource();
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
class Resource{
private String name;
private int sex;
private boolean flag = false;
public synchronized void set(String name, int sex){
if(flag){
try{this.wait();}catch(Exception e){}
}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out(){
if(!flag){
try{this.wait();}catch(Exception e){}
}
System.out.println(name + "..." + sex);
flag = false;
this.notify();
}
}
class Input implements Runnable{
Resource r;
Input(Resource r){
this.r = r;
}
public void run(){
int x = 0;
while(true){
if(x == 0){
r.set("A", 0);
}else{
r.set("B", 1);
}
x = (x + 1)%2;
}
}
}
class Output implements Runnable{
Resource r;
Output(Resource r){
this.r = r;
}
public void run(){
while(true){
r.out();
}
}
}
修正 多生产者及多消费者时,产生的异步错误,使用notifyAll()
class Hello{
public static void main(String[] args){
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource{
private String name;
private int count = 1;
private boolean flag = false;
public synchronized void set(String name){
while(flag){
try{this.wait();}catch(Exception e){}
}
this.name = name + "--" + count++;
System.out.println(Thread.currentThread().getName() + "---生产者---" + this.name);
flag = true;
this.notifyAll();
}
public synchronized void out(){
while(!flag){
try{this.wait();}catch(Exception e){}
}
System.out.println(Thread.currentThread().getName() + "消费者" + this.name);
flag = false;
this.notifyAll();
}
}
class Producer implements Runnable{
private Resource res;
Producer(Resource res){
this.res = res;
}
public void run(){
int x = 0;
while(true){
res.set("+A+");
}
}
}
class Consumer implements Runnable{
Resource res;
Consumer(Resource res){
this.res = res;
}
public void run(){
while(true){
res.out();
}
}
}
2. 使用interrupt中断方法。该方法是结束线程的冻结状态,使线程回到运行状态
class Hello{
public static void main(String[] args){
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource{
private String name;
private int count = 1;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void set(String name) throws InterruptedException{
lock.lock();
try{
while(flag){
condition.await();
}
this.name = name + "--" + count++;
System.out.println(Thread.currentThread().getName() + "---生产者---" + this.name);
flag = true;
condition.signalAll();
}finally{
lock.unlock();//一定要关闭资源
}
}
public void out() throws InterruptedException{
lock.lock();
try{
while(!flag){
condition.await();
}
System.out.println(Thread.currentThread().getName() + "消费者" + this.name);
flag = false;
condition.signalAll();
}finally{
lock.unlock();
}
}
}
class Producer implements Runnable{
private Resource res;
Producer(Resource res){
this.res = res;
}
public void run(){
int x = 0;
while(true){
try {
res.set("+A+");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Consumer implements Runnable{
Resource res;
Consumer(Resource res){
this.res = res;
}
public void run(){
while(true){
try {
res.out();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
控制Thread
class Hello{
public static void main(String[] args){
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t2.start();
int num = 0;
while(true){
if(num++ == 60){
//st.changeFlag();
t1.interrupt();
t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName() + ".....num");
}
System.out.println("over");
}
}
class StopThread implements Runnable{
private boolean flag = true;
public synchronized void run(){
while(flag){
try{
wait();//wait后 其他thread就可以进入此 synchronized 方法
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName() + ".....Excetpion");
flag =false;
}
System.out.println(Thread.currentThread().getName() + ".....run");
}
}
public void changeFlag(){
flag = false;
}
}
setDaemon()当主线程结束后,也会让调用此方法的线程结束
class Hello{
public static void main(String[] args){
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.setDaemon(true);
t2.setDaemon(true);
t1.start();
t2.start();
int num = 0;
while(true){
if(num++ == 60){
//st.changeFlag();
t1.interrupt();
t2.interrupt();
break;
}
System.out.println(Thread.currentThread().getName() + ".....num");
}
System.out.println("over");
}
}
class StopThread implements Runnable{
private boolean flag = true;
public synchronized void run(){
while(flag){
try{
wait();
}catch(InterruptedException e){
System.out.println(Thread.currentThread().getName() + ".....Excetpion");
flag =false;
}
System.out.println(Thread.currentThread().getName() + ".....run");
}
}
public void changeFlag(){
flag = false;
}
}
join()
当a线程执行到了b线程的join()方法时,a就会等待,等b线程执行完,a才会执行
class Hello{
public static void main(String[] args) throws InterruptedException{
Demo st = new Demo();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t1.join();//t1向cpu抢夺执行权 做完才给主线程
t2.start();
for(int x = 0; x <5; x++){
System.out.println("main ..." + x);
}
System.out.println("over");
}
}
class Demo implements Runnable{
public void run(){
for(int x = 0; x <5; x++){
System.out.println(Thread.currentThread().getName() + "..." + x);
}
}
}
执行结果:
//setPriority();、toString();
class Hello{
public static void main(String[] args) throws InterruptedException{
Demo st = new Demo();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t1.setPriority(Thread.MAX_PRIORITY);//10
t2.start();
for(int x = 0; x <80; x++){
System.out.println("main ..." + x);
}
System.out.println("over");
}
}
class Demo implements Runnable{
public void run(){
for(int x = 0; x <70; x++){
System.out.println(Thread.currentThread().toString() + "..." + x);.
}
}
}
//yeild();
class Hello{
public static void main(String[] args) throws InterruptedException{
Demo st = new Demo();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.start();
t2.start();
for(int x = 0; x <80; x++){
System.out.println("main ..." + x);
}
System.out.println("over");
}
}
class Demo implements Runnable{
public void run(){
for(int x = 0; x <70; x++){
System.out.println(Thread.currentThread().toString() + "..." + x);
Thread.yield();//暂停当前正在执行的线程对象,并执行其他线程。
}
}
}
使用封装方法产生线程去执行特定方法,提高并行性
class Hello{
public static void main(String[] args) throws InterruptedException{
new Thread(){
public void run(){
for(int x = 0; x < 100; x++){
System.out.println(Thread.currentThread().getName() + "..." + x);
}
}
}.start();
for(int x = 0; x < 100; x++){
System.out.println(Thread.currentThread().getName() + "..." + x);
}
Runnable r = new Runnable(){
public void run(){
for(int x = 0; x < 100; x++){
System.out.println(Thread.currentThread().getName() + "..." + x);
}
}
};
new Thread(r).start();
}
}
原文:大专栏 JAVA thread
标签:demo notifyall resource ble except 恢复 rgs 结束 实现
原文地址:https://www.cnblogs.com/petewell/p/11526599.html