摘要:一创建线程的方式更可以体现面向对象的思想,线程和代码隔离二定时器代码代码三线程同步通信技术子线程次,然后主线程次,然后子线程次,然后主线程次。如果有多个线程同时到达点了,那这个数据要写入多次,这是不对的实际失去
一、创建线程的方式
package cn.itcast.heima2; public class TraditionalThread { public static void main(String[] args) { // TODO Auto-generated method stub Thread thread = new Thread() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); System.out.println(this.getName()); } } }; thread.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()); } } }); thread2.start();//更可以体现面向对象的思想,线程和代码隔离 new Thread(new Runnable() { @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("runnable:"+Thread.currentThread().getName()); } } }){ @Override public void run() { while (true) { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("thread:"+Thread.currentThread().getName()); } } }.start(); } }二、定时器
代码1:
package cn.itcast.heima2; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { public static void main(String[] args) { new Timer().schedule(new TimerTask() { @Override public void run() { System.out.println("OUTER:boom!"); } },10000,3000); while(true){ System.out.println(Calendar.getInstance().get(Calendar.SECOND)); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代码2:
package cn.itcast.heima2; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask; public class TraditionalTimerTest { static class MyTimerTask extends TimerTask{ static int count=0; @Override public void run() { // TODO Auto-generated method stub count=(count+1)%2; System.out.println("OUTER:boom!"); new Timer().schedule(new MyTimerTask(), 2000+2000*count); } } public static void main(String[] args) { new Timer().schedule(new MyTimerTask(),2000); while(true){ System.out.println(Calendar.getInstance().get(Calendar.SECOND)); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }三、线程同步通信技术
子线程10次,然后主线程100次,然后子线程10次,然后主线程100次。循环50次
package cn.itcast.heima2; public class TraditionalThreadComunication { public static void main(String[] args) { // TODO Auto-generated method stub Business business = new TraditionalThreadComunication().new Business(); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub for (int i = 0; i < 50; i++) { business.sub(i); } } }).start(); for (int i = 0; i < 50; i++) { business.main(i); } } class Business { private boolean bShouldSub=true; public synchronized void sub(int i) { if(!bShouldSub){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (int j = 0; j < 10; j++) { System.out.println("sub thread sequence:" + j + " loop of " + i); } bShouldSub=false; this.notify(); } public synchronized void main(int i) { if(bShouldSub){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } for (int j = 0; j < 100; j++) { System.out.println("main thread sequence:" + j + " loop of " + i); } bShouldSub=true; this.notify(); } } }四、线程范围内共享变量的概念与作用
如下代码中会出现问题
package cn.itcast.heima2; import java.util.HashMap; import java.util.Random; public class ThreadScopeShareData { private static int data = 0; private static HashMapthreadData = new HashMap<>(); public static void main(String[] args) { for (int i = 0; i < 2; i++) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub data = new Random(System.currentTimeMillis()).nextInt(); System.out.println(Thread.currentThread().getName() + " has put data :" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); } } static class A { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data); } } static class B { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data); } } }
解决方法:
package cn.itcast.heima2; import java.util.HashMap; import java.util.Random; public class ThreadScopeShareData { private static int data = 0; static long[] seed = new long[] { 12345612, 654321 }; private static HashMap五、读写锁技术的妙用threadData = new HashMap<>(); public static void main(String[] args) { new Thread(new ThreadScopeShareData.MyRunnable() { @Override public void run() { // TODO Auto-generated method stub data = MyRunnable.rd.nextInt(); System.out.println(Thread.currentThread().getName() + " has put data :" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); new Thread(new ThreadScopeShareData.MyRunnable() { @Override public void run() { // TODO Auto-generated method stub data = MyRunnable.rd.nextInt(); System.out.println(Thread.currentThread().getName() + " has put data :" + data); threadData.put(Thread.currentThread(), data); new A().get(); new B().get(); } }).start(); } static class A { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("A from " + Thread.currentThread().getName() + " has put data :" + data); } } static class B { public void get() { int data=threadData.get(Thread.currentThread()); System.out.println("B from " + Thread.currentThread().getName() + " has put data :" + data); } } abstract static class MyRunnable implements Runnable { static Random rd = new Random(); public abstract void run(); } }
一个线程在写的时候其他线程都不能打断写入过程,就是写入的时候不能读取
package cn.itcast.heima2; import java.util.Random; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class ReadWriteLockTest { static Random rd = new Random(); public static void main(String[] args) { final Queue3 q3 = new Queue3(); for (int i = 0; i < 3; i++) { new Thread() { public void run() { while (true) { q3.get(); } } }.start(); new Thread() { public void run() { while (true) { q3.put(rd.nextInt(10000)); } } }.start(); } } } class Queue3 { private Object data = null;// 共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。 ReadWriteLock rwl = new ReentrantReadWriteLock(); public void get() { rwl.readLock().lock(); try { System.out.println(Thread.currentThread().getName() + " be ready to read data!"); Thread.sleep((long) (Math.random() * 1000)); System.out.println(Thread.currentThread().getName() + " have read data :" + data); } catch (InterruptedException e) { e.printStackTrace(); } finally { rwl.readLock().unlock(); } } public void put(Object data) { rwl.writeLock().lock(); try { System.out.println(Thread.currentThread().getName() + " be ready to write data!"); Thread.sleep((long) (Math.random() * 1000)); this.data = data; System.out.println(Thread.currentThread().getName() + " have write data: " + data); } catch (InterruptedException e) { e.printStackTrace(); } finally { rwl.writeLock().unlock(); } } }
package cn.itcast.heima2; import java.util.HashMap; import java.util.Map; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; public class CacheDemo { private Mapcache = new HashMap (); public static void main(String[] args) { // TODO Auto-generated method stub } private ReadWriteLock rwl = new ReentrantReadWriteLock(); public Object getData(String key){ rwl.readLock().lock(); Object value = null; try{ value = cache.get(key); if(value == null){ rwl.readLock().unlock(); rwl.writeLock().lock();//A try{ if(value==null){//如果有多个线程同时到达A点了,那这个数据要写入多次,这是不对的 value = "aaaa";//实际失去queryDB(); } }finally{ rwl.writeLock().unlock(); } rwl.readLock().lock(); } }finally{ rwl.readLock().unlock(); } return value; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/67508.html
摘要:在中一般来说通过来创建所需要的线程池,如高并发原理初探后端掘金阅前热身为了更加形象的说明同步异步阻塞非阻塞,我们以小明去买奶茶为例。 AbstractQueuedSynchronizer 超详细原理解析 - 后端 - 掘金今天我们来研究学习一下AbstractQueuedSynchronizer类的相关原理,java.util.concurrent包中很多类都依赖于这个类所提供的队列式...
摘要:在中一般来说通过来创建所需要的线程池,如高并发原理初探后端掘金阅前热身为了更加形象的说明同步异步阻塞非阻塞,我们以小明去买奶茶为例。 AbstractQueuedSynchronizer 超详细原理解析 - 后端 - 掘金今天我们来研究学习一下AbstractQueuedSynchronizer类的相关原理,java.util.concurrent包中很多类都依赖于这个类所提供的队列式...
摘要:表示的是两个,当其中任意一个计算完并发编程之是线程安全并且高效的,在并发编程中经常可见它的使用,在开始分析它的高并发实现机制前,先讲讲废话,看看它是如何被引入的。电商秒杀和抢购,是两个比较典型的互联网高并发场景。 干货:深度剖析分布式搜索引擎设计 分布式,高可用,和机器学习一样,最近几年被提及得最多的名词,听名字多牛逼,来,我们一步一步来击破前两个名词,今天我们首先来说说分布式。 探究...
阅读 938·2023-04-25 23:50
阅读 1912·2021-11-19 09:40
阅读 539·2019-08-30 13:50
阅读 2694·2019-08-29 17:11
阅读 1020·2019-08-29 16:37
阅读 2964·2019-08-29 12:54
阅读 2776·2019-08-28 18:17
阅读 2608·2019-08-26 16:55