class MyThread extends Thread{ int j=20; public void run(){ for (int i = 0; i < 20; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(this.getName()+",i="+j--); } } }
然后main函数中创建:
class MyThread extends Thread{ int j=20; public void run(){ for (int i = 0; i < 20; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(this.getName()+",i="+j--); } } }
第二种方法:
class MyRunnable implements Runnable{ int j=20; @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName()+",j="+this.j--); } } }
main函数中:
MyRunnable myRunnable = new MyRunnable(); Thread t1 = new Thread(myRunnable); Thread t2 = new Thread(myRunnable); t1.start(); t2.start();
class MyRunnable implements Runnable{ static int j=10000; @Override public void run() { for (int i = 0; i < 5000; i++) { System.out.println(j--); } } }
main函数中:
MyRunnable myRunnable = new MyRunnable(); Thread t1 = new Thread(myRunnable); Thread t2 = new Thread(myRunnable); t1.start(); t2.start();
可以看到,这里同时两个线程同时对共享变量j进行访问,并且减1,但最后的输出结果却是:
48 47 46
class MyRunnable implements Runnable{ static int j=10000; @Override public synchronized void run() { for (int i = 0; i < 5000; i++) { System.out.println(j--); } } }
main中创建两个线程,测试多次的结果都是:
3 2 1
try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }
class MyThreadd extends Thread{ public MyThreadd(String name) { // TODO Auto-generated constructor stub super(name); } public void run(){ synchronized (this) { System.out.println(Thread.currentThread().getName()+" notify a thread"); notify(); } while(true); }
main中:
MyThreadd t1 = new MyThreadd("t1"); synchronized (t1) { System.out.println(Thread.currentThread().getName()+" start t1"); t1.start(); System.out.println(Thread.currentThread().getName()+" wait"); t1.wait(); System.out.println(Thread.currentThread().getName()+" waked up"); }
class MyRuu extends Thread{ public MyRuu(String name) { super(name); } public void run() { System.out.println(Thread.currentThread().getName()); //while(true); } }
main函数中:
MyRuu myRuu = new MyRuu("t1"); System.out.println(Thread.currentThread().getName()+" start t1"); myRuu.start(); System.out.println(Thread.currentThread().getName() +" join"); myRuu.join(); System.out.println(Thread.currentThread().getName() +" waked up");
运行结果:
main start t1 main join t1 main waked up
class MyRun extends Thread{ Object obj; public MyRun(String name,Object obj) { // TODO Auto-generated constructor stub super(name); this.obj = obj; } public void run(){ // synchronized (obj) { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()+ " i="+i); if(i%2 == 0) Thread.yield(); } // } } }
main函数中:
Object obj = new Object(); // TODO Auto-generated method stub MyRun t1 = new MyRun("t1",obj); MyRun t2 = new MyRun("t2",obj); t1.start(); t2.start();
结果:
t1 i=0 t2 i=0 t1 i=1 t2 i=1 t2 i=2 t1 i=2 t2 i=3 t2 i=4 t1 i=3 t1 i=4 t2 i=5 t2 i=6 t1 i=5 t1 i=6 t2 i=7 t2 i=8 t1 i=7 t1 i=8 t2 i=9 t1 i=9
class RunInt extends Thread{ public void run() { while(!this.isInterrupted()){ synchronized (this) { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName()+" i="+i); } try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block System.out.println(Thread.currentThread().getName()+" interrupted!"); break; } } } System.out.println(Thread.currentThread().getName()+" dead"); } }
main中:
RunInt r1 = new RunInt(); r1.start(); Thread.yield(); synchronized (r1) { System.out.println(Thread.currentThread().getName()+" intertupt r1"); r1.interrupt(); }
结果
main intertupt r1 Thread-0 i=0 Thread-0 i=1 Thread-0 i=2 Thread-0 i=3 Thread-0 i=4 Thread-0 i=5 Thread-0 i=6 Thread-0 i=7 Thread-0 i=8 Thread-0 i=9 Thread-0 interrupted! Thread-0 dead
Thread t1 = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub synchronized (A) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (B) { System.out.println("haha"); } } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub synchronized (B) { synchronized (A) { System.out.println("xixi"); } } } }); t1.start(); t2.start();
class Depot{ private int capacity; private int size=0; public Depot(int c) { // TODO Auto-generated constructor stub this.capacity = c; } public synchronized void product(int count) throws InterruptedException{ while(count>0){ if(size >= capacity) wait(); //真实生产数量 int realcount = (capacity-size)>=count?count:(capacity-size); System.out.print(Thread.currentThread().getName()+"--本次想要生产:"+count+",本次实际生产:"+realcount); //下次生产数量 count = count - realcount; //仓库剩余 size += realcount; System.out.println(",下次想要生产:"+count+",仓库真实容量:"+size); notifyAll(); } } public synchronized void comsume(int count) throws InterruptedException { while(count>0){ if(size <= 0) wait(); //真实消费数量 int realcount = (size>=count)?count:size; System.out.print(Thread.currentThread().getName()+"--本次想要消费:"+count+",本次真实消费:"+realcount); //下次消费数量 count = count - realcount; //仓库剩余 size -= realcount; System.out.println("下次想要消费:"+count+",仓库剩余:"+size); notify(); } } }
生产者代码:
class Producer { Depot depot; public Producer(Depot depot) { // TODO Auto-generated constructor stub this.depot = depot; } public void produce(final int count) { new Thread(){ public void run() { try { depot.product(count); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); } }
消费者代码:
class Consumer{ Depot depot; public Consumer(Depot depot) { // TODO Auto-generated constructor stub this.depot = depot; } public void consume(final int count) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { depot.comsume(count); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }
main中:
Depot depot = new Depot(100); Producer producer = new Producer(depot); Consumer consumer = new Consumer(depot); producer.produce(60); producer.produce(50); producer.produce(30); consumer.consume(50); consumer.consume(110); producer.produce(40);
结果:
Thread-0--本次想要生产:60,本次实际生产:60,下次想要生产:0,仓库真实容量:60 Thread-1--本次想要生产:50,本次实际生产:40,下次想要生产:10,仓库真实容量:100 Thread-4--本次想要消费:110,本次真实消费:100下次想要消费:10,仓库剩余:0 Thread-1--本次想要生产:10,本次实际生产:10,下次想要生产:0,仓库真实容量:10 Thread-4--本次想要消费:10,本次真实消费:10下次想要消费:0,仓库剩余:0 Thread-5--本次想要生产:40,本次实际生产:40,下次想要生产:0,仓库真实容量:40 Thread-2--本次想要生产:30,本次实际生产:30,下次想要生产:0,仓库真实容量:70 Thread-3--本次想要消费:50,本次真实消费:50下次想要消费:0,仓库剩余:20
class Pro1{ private BlockingQueue
blockingQueue1; public Pro1(BlockingQueue
blockingQueue) { // TODO Auto-generated constructor stub this.blockingQueue1 = blockingQueue; } public void produce(final int count) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < count; i++) { try { Thread.sleep(100); blockingQueue1.put(100); System.out.println("生产者,仓库剩余容量"+blockingQueue1.size()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
消费者:
class Con1{ private BlockingQueue
blockingQueue; public Con1(BlockingQueue
blockingQueue) { // TODO Auto-generated constructor stub this.blockingQueue = blockingQueue; } public void consume(final int count) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < count; i++) { try { Thread.sleep(100); blockingQueue.take(); System.out.println("消费者,本次仓库剩余:"+blockingQueue.size()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }).start(); } }
main函数:
BlockingQueue
blockingQueue = new ArrayBlockingQueue
(5); Pro1 pro1 = new Pro1(blockingQueue); Con1 con1 = new Con1(blockingQueue); pro1.produce(10); con1.consume(7);
结果:
消费者,本次仓库剩余:0 生产者,仓库剩余容量0 生产者,仓库剩余容量1 消费者,本次仓库剩余:0 生产者,仓库剩余容量1 消费者,本次仓库剩余:0 生产者,仓库剩余容量0 消费者,本次仓库剩余:0 生产者,仓库剩余容量1 消费者,本次仓库剩余:0 生产者,仓库剩余容量1 消费者,本次仓库剩余:0 生产者,仓库剩余容量1 消费者,本次仓库剩余:0 生产者,仓库剩余容量1 生产者,仓库剩余容量2 生产者,仓库剩余容量3
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218387.html原文链接:https://javaforall.net
