并发编程的艺术——chap1

原文链接:https://blog.csdn.net/ftell/article/details/79353571#1  线程概念

1 线程概念

Java为创建和运行线程以及锁定资源以防止冲突提供了非常好的支持。你可以在程序中创建额外的线程以执行并发任务。在Java中,每个任务都是Runnable接口的实例,也称为一个runnable对象。一个线程实质上是一个对象,它为任务的执行提供便利。

public class TaskClass implements Runnable {
    public TaskClass(...) {
    }
    // 实现Runnable中的run方法
    public void run() {
        // 告诉系统如何运行自定义线程
    }
}
 
public class Client {
    public void someMethod() {
        // 创建TaskClass的实例
        TaskClass task = new TaskClass(...);
        // 创建线程
        Thread thread = new Thread(task);
        // 启动线程
        thread.start();
    }
}

2 创建任务和线程

task类必须实现 Runnable接口,task必须从线程执行。
task是对象,为了创建task,必须首先为task定义一个实现了Runnable接口的类。Runnable接口相当简单,只包含了一个Run方法。

task必须在线程中执行,Thread类含有用于创建线程的构造函数,以及很多用于控制线程的方法,创建task的线程:

Thread thread = new Thread(task);

然后调用start()方法告诉JVM,线程已经可以运行:

thread.start();

JVM 通过调用task的run()方法执行task. 下面的例子,创建3个线程,分别打印’a’ 100 次,打印’b’ 100 次, 以及打印 0 ~ 100 之间的整数:

public class TaskThreadDemo {
    public static void main(String[] args) {
        // 创建task
        Runnable printA = new PrintChar('a', 100);  // Runnable 改成 PrintChar 也可以
        Runnable printB = new PrintChar('b', 100);  // Runnable 改成 PrintChar 也可以
        Runnable print100 = new PrintNum(100);  // Runnable 改成 PrintNum 也可以
        // 创建线程
        Thread thread1 = new Thread(printA);
        Thread thread2 = new Thread(printB);
        Thread thread3 = new Thread(print100);
        // 启动线程
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
 
// 打印指定次数字符的 task
 class PrintChar implements Runnable {
    private char charToPrint; // The character to print
    private int times; // The number of times to repeat
 
    /** Construct a task with a specified character and number of
    * times to print the character
    */
    public PrintChar(char c, int t) {
        charToPrint = c;
        times = t;
    }
 
    @Override /** Override the run() method to tell the system
    * what task to perform
    */
    public void run() {
        for (int i = 0; i < times; i++) {
            System.out.print(charToPrint);
        }
    }
}
 
// The task class for printing numbers from 1 to n for a given n
class PrintNum implements Runnable {
    private int lastNum;
 
    /** Construct a task for printing 1, 2, ..., n */
    public PrintNum(int n) {
        lastNum = n;
    }
 
    @Override /** Tell the thread how to run */
    public void run() {
        for (int i = 1; i <= lastNum; i++) {
            System.out.print(" " + i);
        }
    }
}

3 Thread 类

Thread 类包含了创建线程的构造函数以及控制线程的方法
Thread 类实现接口 Runnable:

                      << interface >>

java.lang.Thread -> java.lang.Runnable

因为Thread实现Runnable,因此可以定义类继承Thread并实现Run方法:

// 自定义thread类
public class CustomThread extends Thread {
    public CustomThread(...) {
    }
    // 重写Runnable里的run方法
    public void run() {
        // 告诉系统如何执行这个task
    }
}
 
// 自定义类
public class Client {
    public void someMethod() {
    // 创建一个线程
    CustomThread thread1 = new CustomThread(...);
    // 启动线程
    thread1.start();
    // 创建另一个线程
    CustomThread thread2 = new CustomThread(...);
    // 启动线程
    thread2.start();
    }
}

例子:并行和串行

//package chapter01;

/**
 * 并发和单线程执行测试
 * 
 * @author tengfei.fangtf
 * @version $Id: ConcurrencyTest.java, v 0.1 2014-7-18 下午10:03:31 tengfei.fangtf Exp $
 */
public class newtest {

    /** 执行次数 */
    private static final long count = 40000l;

    public static void main(String[] args) throws InterruptedException {
        //并发计算
        concurrency();
        //单线程计算
        serial();
    }

    private static void concurrency() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() 
            {
                int a = 0;
                for (long i = 0; i < count; i++) {
                    a += 5;
                }
                System.out.println(a);
            }
        });
        thread.start(); //启动线程
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        thread.join();
        long time = System.currentTimeMillis() - start;
        System.out.println("concurrency :" + time + "ms,b=" + b);
    }

    private static void serial() {
        long start = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < count; i++) {
            a += 5;
        }
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("serial:" + time + "ms,b=" + b + ",a=" + a);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CHAP(Challenge-Handshake Authentication Protocol)挑战-回应式身份验证协议是一种常用的身份验证协议,常用于计算机网络中的远程访问。基于openssl库编程实现CHAP应答涉及以下步骤: 1. 导入openssl库:在编程环境中,首先需要导入openssl库,以便使用其中的加密和哈希算法功能。 2. 接收挑战信息:从网络或其他通信渠道接收CHAP挑战信息。挑战信息通常包括用户名、挑战字节序列和预共享密钥等。 3. 生成应答:使用openssl库中的加密和哈希函数,将挑战信息与预共享密钥进行处理生成应答。具体操作包括: a. 使用用户名和预共享密钥在openssl库中计算出一个MD5散列摘要,作为一个32字节的值。 b. 将挑战字节序列附加在MD5摘要的末尾,形成一个新的数据序列。 c. 再次使用用户名和预共享密钥计算新数据序列的MD5摘要,作为最终的应答。 4. 送应答:将生成的应答信息送给起方,以完成身份验证过程。 在基于openssl库进行CHAP应答编程时,需要注意以下事项: - 确保openssl库已正确安装并在编程环境中正确导入。 - 对用户名和预共享密钥进行适当的存储和管理,以保证安全性。 - 根据标准CHAP协议,应答的生成方法可能会有一些特定的要求和约定,需要根据具体情况进行适配。 基于openssl库编程实现CHAP应答,可以有效地提高网络通信的安全性,防止身份伪造和数据泄露的风险。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值