多线程篇三:线程同步

 1.线程同步

package com.test.synchronizeds;

//线程同步
public class TraditionThreadSynchronized {

    public void init(){
        final Outputer o=new Outputer();
        
        //线程1
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    o.output1("chenxiaobing");
                }
            }
        }).start();
        
        //线程2
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    o.output1("donyanxia");
                }
            }
        }).start();
    }
    public static void main(String[] args) {
        new TraditionThreadSynchronized().init();
    }
    
    static class Outputer{
        
        //方法1
        public void output1(String name){
            int len=name.length();
            synchronized(this){//钥匙必须相同 :synchronized中的的变量,必须是针对所有线程来讲都是一样的,此时才能打达到线程同步的效果.
                for(int i=0;i<len;i++){
                    System.out.print(name.charAt(i));
                }
                System.out.println();
            }
            
        }
        
        //方法2 output2上的synchronized检测的对象是当前这个对象,因此如果线程1调用output1,线程2调用output2也能同步
        public synchronized void output2(String name){
            int len=name.length();
            for(int i=0;i<len;i++){
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }
        
        //方法3 output3与output1是不会线程同步的,但是如果将output1中的this改为Outputer.class,则两者检查的都是当前类的字节码,会达到同步效果
        public static synchronized void output3(String name){
            int len=name.length();
            for(int i=0;i<len;i++){
                System.out.print(name.charAt(i));
            }
            System.out.println();
        }
    }
}

 2.线程同步、交互

package com.test.synchronizeds;

/**
 * 子线程循环10次,主线程循环20次,子线程循环10次,主线程循环20次,如此交替循环50次
 * @author Administrator
 *
 */
public class TraditionThreadCommunication {

    
    public static void main(String[] args) {
        final Bussines b=new Bussines();
        
        //子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i<=50;i++){
                    b.sub(i);
                }
            }
        }).start();
        
        //主线程
        for(int i=1;i<=50;i++){
            b.main(i);
        }
    }
    
    static class Bussines{
        private boolean bshouldsub=true;
        public synchronized void main(int loop){
            while(bshouldsub){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for(int i=1;i<=20;i++){
                System.out.println("main thread sequece of"+i+" loop of"+loop);
            }
            bshouldsub = true;
            this.notify();
        }
        
        public synchronized void sub(int loop){
            while(!bshouldsub){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            for(int i=1;i<=10;i++){
                System.out.println("sub thread sequece of"+i+" loop of"+loop);
            }
            bshouldsub = false;
            this.notify();
        }
    }
}

 

转载于:https://www.cnblogs.com/brant/p/5958192.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
包含代码和文档 实验一 实验内容 编写程序,演示多进程并发执行和进程软中断、管道通信。 父进程使用系统调用pipe( )建立一个管道,然后使用系统调用fork()创建两个子进程,子进程1和子进程2; 子进程1每隔1秒通过管道向子进程2发送数据: I send you x times. (x初值为1,每次发送后做加一操作) 子进程2从管道读出信息,并显示在屏幕上。 父进程用系统调用signal()捕捉来自键盘的中断信号(即按Ctrl+C键);当捕捉到中断信号后,父进程用系统调用Kill()向两个子进程发出信号,子进程捕捉到信号后分别输出下列信息后终止: Child Process l is Killed by Parent! Child Process 2 is Killed by Parent! 父进程等待两个子进程终止后,释放管道并输出如下的信息后终止 Parent Process is Killed! 实验二 实验内容 过Linux多线程与信号灯机制,设计并实现计算机线程与I/O线程共享缓冲区的同步与通信。 程序要求:两个线程,共享公共变量a 线程1负责计算(1到100的累加,每次加一个数) 线程2负责打印(输出累加的中间结果) 实验 实验内容 利用多个共享内存(有限空间)构成的环形缓冲,将源文件复制到目标文件,实现两个进程的誊抄。 实验四 实验内容 1、(1)Blink程序的编译和下载 (2)给Blink程序加入printf,在每次定时器事件触发点亮LED的同时通过串口显示信息 (3)修改BLink程序,只使用一个Timer,个LED灯作为3位的二进制数表示(亮灯为1,不亮为0),按照0-7的顺序循环显示,同时将数值显示在终端上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值