Java的多线程-实现多线程及线程的同步

欢迎访问: http://www.ptcms.cn


Java的多线程-实现多线程及线程的同步 一. 实现多线程    1. 虚假的多线程  例1:    public class TestThread { int i=0, j=0; public void go(int flag){ while(true){ try{ Thread.sleep(100); } catch(InterruptedException e){ System.out.println("Interrupted"); } if(flag==0) i++; System.out.println("i=" + i); } else{ j++; System.out.println("j=" + j); } } } public static void main(String[] args){ new TestThread().go(0); new TestThread().go(1); }}     上面程序的运行结果为:  i=1   i=2   i=3   。。。  结果将一直打印出I的值。我们的意图是当在while循环中调用sleep()时,另一个线程就将起动,打印出j的值,但结果却并不是这样。关于sleep()为什么不会出现我们预想的结果,在下面将讲到。  2. 实现多线程  通过继承class Thread或实现Runnable接口,我们可以实现多线程  2.1 通过继承class Thread实现多线程  class Thread中有两个最重要的函数run()和start()。  1) run()函数必须进行覆写,把要在多个线程中并行处理的代码放到这个函数中。  2) 虽然run()函数实现了多个线程的并行处理,但我们不能直接调用run()函数,而是通过调用start()函数来调用run()函数。在调用start()的时候,start()函数会首先进行与多线程相关的初始化(这也是为什么不能直接调用run()函数的原因),然后再调用run()函数。  例2:    public class TestThread extends Thread{ private static int threadCount = 0; private int threadNum = ++threadCount; private int i = 5; public void run(){ while(true){ try{ Thread.sleep(100);  } catch(InterruptedException e){ System.out.println("Interrupted"); } System.out.println("Thread " + threadNum + " = " + i); if(--i==0) return; } } public static void main(String[] args){ for(int i=0; i<5; i++) new TestThread().start(); }}     运行结果为:  Thread 1 = 5   Thread 2 = 5   Thread 3 = 5   Thread 4 = 5   Thread 5 = 5   Thread 1 = 4   Thread 2 = 4   Thread 3 = 4   Thread 4 = 4   Thread 1 = 3   Thread 2 = 3   Thread 5 = 4   Thread 3 = 3   Thread 4 = 3   Thread 1 = 2   Thread 2 = 2   Thread 5 = 3   Thread 3 = 2   Thread 4 = 2   Thread 1 = 1   Thread 2 = 1   Thread 5 = 2   Thread 3 = 1   Thread 4 = 1   Thread 5 = 1   从结果可见,例2能实现多线程的并行处理。  **:在上面的例子中,我们只用new产生Thread对象,并没有用reference来记录所产生的Thread对象。根据垃圾回收机制,当一个对象没有被reference引用时,它将被回收。但是垃圾回收机制对Thread对象“不成立”。因为每一个Thread都会进行注册动作,所以即使我们在产生Thread对象时没有指定一个reference指向这个对象,实际上也会在某个地方有个指向该对象的reference,所以垃圾回收器无法回收它们。  3) 通过Thread的子类产生的线程对象是不同对象的线程    class TestSynchronized extends Thread{ public TestSynchronized(String name){ super(name); } public synchronized static void prt(){ for(int i=10; i<20; i++){ System.out.println(Thread.currentThread().getName() + " : " + i); try{ Thread.sleep(100); } catch(InterruptedException e){ System.out.println("Interrupted"); } } } public synchronized void run(){ for(int i=0; i<3; i++){ System.out.println(Thread.currentThread().getName() + " : " + i); try{ Thread.sleep(100); } catch(InterruptedException e){ System.out.println("Interrupted"); } } }}public class TestThread{ public static void main(String[] args){ TestSynchronized t1 = new TestSynchronized("t1"); TestSynchronized t2 = new TestSynchronized("t2"); t1.start(); t1.start(); //(1) //t2.start(); (2) }}     运行结果为:  t1 : 0   t1 : 1   t1 : 2   t1 : 0   t1 : 1   t1 : 2   由于是同一个对象启动的不同线程,所以run()函数实现了synchronized。如果去掉(2)的注释,把代码(1)注释掉,结果将变为:  t1 : 0   t2 : 0   t1 : 1   t2 : 1   t1 : 2   t2 : 2   由于t1和t2是两个对象,所以它们所启动的线程可同时访问run()函数。  2.2 通过实现Runnable接口实现多线程  如果有一个类,它已继承了某个类,又想实现多线程,那就可以通过实现Runnable接口来实现。  1) Runnable接口只有一个run()函数。  2) 把一个实现了Runnable接口的对象作为参数产生一个Thread对象,再调用Thread对象的start()函数就可执行并行操作。如果在产生一个Thread对象时以一个Runnable接口的实现类的对象作为参数,那么在调用start()函数时,start()会调用Runnable接口的实现类中的run()函数。  例3.1:    public class TestThread implements Runnable{ private static int threadCount = 0; private int threadNum = ++threadCount; private int i = 5; public void run(){ while(true){ try{ Thread.sleep(100); } catch(InterruptedException e){ System.out.println("Interrupted"); } System.out.println("Thread " + threadNum + " = " + i); if(--i==0) return; } } public static void main(String[] args){ for(int i=0; i<5; i++) new Thread(new TestThread()).start(); //(1) }}     运行结果为:  Thread 1 = 5   Thread 2 = 5   Thread 3 = 5   Thread 4 = 5   Thread 5 = 5   Thread 1 = 4   Thread 2 = 4   Thread 3 = 4   Thread 4 = 4   Thread 4 = 3   Thread 5 = 4   Thread 1 = 3   Thread 2 = 3   Thread 3 = 3   Thread 4 = 2   Thread 5 = 3   Thread 1 = 2   Thread 2 = 2   Thread 3 = 2   Thread 4 = 1   Thread 5 = 2   Thread 1 = 1   Thread 2 = 1   Thread 3 = 1   Thread 5 = 1   例3是对例2的修改,它通过实现Runnable接口来实现并行处理。代码(1)处可见,要调用TestThread中的并行操作部分,要把一个TestThread对象作为参数来产生Thread对象,再调用Thread对象的start()函数。  3) 同一个实现了Runnable接口的对象作为参数产生的所有Thread对象是同一对象下的线程。  例3.2:    package mypackage1;public class TestThread implements Runnable{ public synchronized void run(){ for(int i=0; i<5; i++){ System.out.println(Thread.currentThread().getName() + " : " + i); try{ Thread.sleep(100); } catch(InterruptedException e){ System.out.println("Interrupted"); } } } public static void main(String[] args){ TestThread testThread = new TestThread(); for(int i=0; i<5; i++) //new Thread(testThread, "t" + i).start(); (1) new Thread(new TestThread(), "t" + i).start(); (2) }}     运行结果为:  t0 : 0   t1 : 0   t2 : 0   t3 : 0   t4 : 0   t0 : 1   t1 : 1   t2 : 1   t3 : 1   t4 : 1   t0 : 2   t1 : 2   t2 : 2   t3 : 2   t4 : 2   t0 : 3   t1 : 3   t2 : 3   t3 : 3   t4 : 3   t0 : 4   t1 : 4   t2 : 4   t3 : 4   t4 : 4   由于代码(2)每次都是用一个新的TestThread对象来产生Thread对象的,所以产生出来的Thread对象是不同对象的线程,所以所有Thread对象都可同时访问run()函数。如果注释掉代码(2),并去掉代码(1)的注释,结果为:  t0 : 0   t0 : 1   t0 : 2   t0 : 3   t0 : 4   t1 : 0   t1 : 1   t1 : 2   t1 : 3   t1 : 4   t2 : 0   t2 : 1   t2 : 2   t2 : 3   t2 : 4   t3 : 0   t3 : 1   t3 : 2   t3 : 3   t3 : 4   t4 : 0   t4 : 1   t4 : 2   t4 : 3   t4 : 4   由于代码(1)中每次都是用同一个TestThread对象来产生Thread对象的,所以产生出来的Thread对象是同一个对象的线程,所以实现run()函数的同步。    二. 共享资源的同步    1. 同步的必要性  例4:    class Seq{ private static int number = 0; private static Seq seq = new Seq(); private Seq() {} public static Seq getInstance(){ return seq; } public int get(){ number++;  //(a) return number; //(b) }}public class TestThread{ public static void main(String[] args){ Seq.getInstance().get(); //(1) Seq.getInstance().get(); //(2) }}     上面是一个取得序列号的单例模式的例子,但调用get()时,可能会产生两个相同的序列号:  当代码(1)和(2)都试图调用get()取得一个唯一的序列。当代码(1)执行完代码(a),正要执行代码(b)时,它被中断了并开始执行代码(2)。一旦当代码(2)执行完(a)而代码(1)还未执行代码(b),那么代码(1)和代码(2)就将得到相同的值。  2. 通过synchronized实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值