- 博客(13)
- 收藏
- 关注
原创 java-多线程的创建
两种方式: 1.继承Thread类 2.实现Runnable接口 1.继承Thread类 第一步:创建一个继承Thread类的子类; 第二步:重写run()方法,方法体为需要实现的功能; 第三步:在main()方法中创建该子类的对象; 第四步:用该对象调用start()方法。 public class MyThread extends Thread{ public static void main(String[] args) { MyThread myThrea
2022-03-18 17:49:36
241
原创 java-生产者消费者
使用Lock public class ProduceConsume { private ReentrantLock lock = new ReentrantLock(); private Condition isFull = lock.newCondition(); private Condition isEmpty = lock.newCondition(); int[] tube = new int[10]; static int Pindex, Cin
2022-03-18 15:04:25
114
原创 java-线程通信
wait(): 当前线程进入阻塞状态,并释放同步监视器 notify():唤醒被wait的一个线程,如果有多个线程被wait,则唤醒优先级高的 notifyAll:唤醒被wait的所有线程 这三个方法必须使用在同步代码块和同步方法中; 这三个方法的调用者必须是同步代码块和同步方法中的监视器,否则会出现异常。 public class Test1 implements Runnable{ private static int num = 1; @Override publ
2022-03-18 15:03:26
243
原创 java-线程同步-Lock
Lock: 接口 ReentrantLock类实现了Lock,常用实现类 构造器分为有参数和无参数,有参数的如果传入true,说明线程是公平的,先到先进,不会出现一个线程刚进入再次进入的现象,传入false则没有上述功能。 1.实例化ReentrantLock 2.调用lock(),必须紧跟在try后面 3.调用unlock() public class LockTest { public static void main(String[] args) { Window
2022-03-18 13:03:52
343
原创 java-线程安全
方式一:同步代码块 synchronized(同步监视器){ 需要被同步的代码 } 说明:操作共享数据的代码,即为需要被同步的代码! 共享数据:多个线程共同操作的变量 同步监视器:俗称锁。任何一个类的对象都可以充当锁。 要求:多个线程必须共用同一把锁。 比如如果是用继承Thread方法,锁需要是静态的。 如果是实现Runnable接口的方式,锁可以使用this充当。 ...
2022-03-17 16:47:49
437
原创 java-快速排序
public class QuickSort { public static void main(String[] args) { int[] arr = {2,3,4,7,0,1,3,5,7,9}; quickSort(arr); System.out.println(Arrays.toString(arr)); } public static void quickSort(int[] arr){ qSort(ar.
2022-03-04 16:47:49
490
原创 java-堆排序
public class HeapSort { public static void main(String[] args) { int[] arr = new int[]{50,10,90,30,70,40,80,60,20}; heapSort(arr); System.out.println(Arrays.toString(arr)); } public static void heapSort(int[] arr){ .
2022-03-04 14:21:46
489
原创 java-希尔排序
public class ShellSort { public static void main(String[] args) { int[] arr = new int[]{3,5,2,7,1,6,4,6,3}; shellSort(arr); System.out.println(Arrays.toString(arr)); } public static void shellSort(int[] arr){ .
2022-03-03 14:02:58
148
原创 java-直接插入排序
public class StraightInsertionSort { public static void main(String[] args) { int[] arr = new int[]{3,5,2,7,1,6,4,6,3}; straightInsertionSort(arr); System.out.println(Arrays.toString(arr)); } public static void straigh.
2022-03-03 13:13:48
219
原创 java-简单选择排序
public class SimpleSelectionSort { public static void main(String[] args) { int[] arr = new int[]{3,5,2,7,1,6,4,6,3}; simpleSelectionSort(arr); System.out.println(Arrays.toString(arr)); } public static void simpleSelect.
2022-03-03 12:45:03
315
原创 java-冒泡排序
public class BubbleSort { public static void main(String[] args) { int[] arr = new int[]{3,5,2,7,1,6,4,6,3}; bubblesort(arr); System.out.println(Arrays.toString(arr)); } public static void bubblesort(int[] arr){ .
2022-03-03 12:23:09
227
原创 Comparator和Comparable接口的使用
一、Comparable接口 自定义类实现Comparable接口,在类中重写compareTo方法,如果调用对象比传入对象大则返回正数,这样排序就是升序 如图为Student类 public class Student implements Comparable{ int age; String name; public Student(int age) { this.age = age; } public Student(int age
2022-02-27 18:37:04
497
原创 java-集合-基础知识
一、Collection接口:单列数据 1.List接口:有序,可重复 ①Vector类:底层是数组,古老实现类,线程安全的,尽量避免使用,执行效率低,扩容请求2倍空间,有一个子类是Stack(栈) ②ArrayList类:底层是数组,主要实现类,线程不安全,执行效率高,扩容请求1.5倍空间 ③LinkedList类:底层是双向链表,插入删除频繁时,建议使用,线程不安全,执行效率高 2.Set接口:无序,不可重复 ①HashSet类:底层为数组,主要实现类,按照Hash算法存储集合中的元素。如果
2022-02-27 15:06:20
384
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人