Java基础

January 08, 2025 作者: pika-chu 分类: Java 浏览: 13 评论: 0

多线程

同时用100个线程向同一个账户中存1块钱


package org.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author dyh
 * @date 2023.08.31 9:22
 */
public class Account {
    private Double money;

    Account() {
        money = 0D;
    }

    public static void main(String[] args) {
        while (true) {
            Account account = new Account();
            testPool(account);
        }
    }

    private static void testPool(Account account) {
        //用100个线程向同一个账户存钱
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        CountDownLatch countDownLatch = new CountDownLatch(100);
        ThreadRun run = new ThreadRun(account, countDownLatch);
        int i = 100;
        while (i-- > 0)
            executorService.submit(run);
        try {
            countDownLatch.await();
            System.out.println("Account的账户余额为:" + account.getMoney());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    private static void testJoin(Account account) {
        //用100个线程向同一个账户存钱
        int i = 100;
        while (i-- > 0) {
            try {
                ThreadRun run = new ThreadRun(account);
                Thread t = new Thread(run);
                t.start();
                t.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("Account的账户余额为:" + account.getMoney());
    }

    private static void testAlive(Account account) {
        //用100个线程向同一个账户存钱
        int i = 100;
        while (i-- > 0) {
            ThreadRun run = new ThreadRun(account);
            Thread t = new Thread(run);
            t.start();
            while (t.isAlive()) {
            }
        }
        System.out.println("Account的账户余额为:" + account.getMoney());
    }

    private static void testYield(Account account) {
        //用100个线程向同一个账户存钱
        int i = 100;
        while (i-- > 0) {
            ThreadRun run = new ThreadRun(account);
            Thread t = new Thread(run);
            t.start();
        }
        while (Thread.activeCount() > 1)
            Thread.yield();
        System.out.println("Account的账户余额为:" + account.getMoney());
    }

    public synchronized double getMoney() {
        return money;
    }

    public synchronized void despoilment(double num) {
        money += num;
    }
}

class ThreadRun implements Runnable {
    CountDownLatch countDownLatch;
    Account account;

    ThreadRun(Account account) {
        this.account = account;
    }

    ThreadRun(Account account, CountDownLatch countDownLatch) {
        this.account = account;
        this.countDownLatch = countDownLatch;
    }

    @Override
    public synchronized void run() {
        account.despoilment(1D);
        countDownLatch.countDown();
//        System.out.println(Thread.currentThread() + "存入1元,余额为:" + account.getMoney());
    }
}

排序

  • 冒泡排序

package com.sort;

import java.util.Scanner;

/**
 * @author dyh
 * @date 2023.08.15 16:45
 */
public class BubbleSort {
    public static void main(String[] args) {
        sort();
    }

    public static void sort() {
        Scanner sc = new Scanner(System.in);
        int temp;
        //从键盘输入5个数字
        int[] arr = new int[5];
        for (int i = 0; i < arr.length ; i++) {
            System.out.println("请输入数字");
            arr[i] = sc.nextInt();
        }
        //冒泡排序
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                //从小到大排列
                if (arr[j] > arr[j + 1]) {
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println("排序后的数组为");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "=======");
        }
        System.out.println();
        for (int a : arr) {
            System.out.print(a + "======");
        }
    }
}
  • 快速排序

package com.sort;

import java.util.Arrays;

/**
 * @author dyh
 * @date 2023.08.31 14:28
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
        quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static int[] quickSort(int[] arr) {
        if (arr == null || arr.length == 0) {
            return arr;
        }
        return quickSort(arr, 0, arr.length - 1);
    }

    private static int[] quickSort(int[] arr, int left, int right) {
        if (left >= right) {
            return arr;
        }
        int pivot = arr[left];
        int i = left, j = right;
        while (i < j) {
            while (i < j && arr[j] >= pivot) {
                j--;
            }
            arr[i] = arr[j];
            while (i < j && arr[i] <= pivot) {
                i++;
            }
            arr[j] = arr[i];
        }
        arr[i] = pivot;
        quickSort(arr, left, i - 1);
        quickSort(arr, i + 1, right);
        return arr;
    }

}

#Java(4)

评论