update course5

master
terry 3 years ago
parent a32aea265e
commit 592e2e2bd4

@ -0,0 +1,22 @@
package com.demo.tank.course5.singleton;
/*
* 饿
* JVM线
*
* */
public class Singleton1 {
private static final Singleton1 INSTANCE = new Singleton1();
private Singleton1() {
}
public static Singleton1 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton1 singleton = Singleton1.getInstance();
System.out.println(singleton);
}
}

@ -0,0 +1,23 @@
package com.demo.tank.course5.singleton;
/*
* 1
* */
public class Singleton2 {
private static final Singleton2 INSTANCE;
static {
INSTANCE = new Singleton2();
}
private Singleton2() {
}
public static Singleton2 getInstance() {
return INSTANCE;
}
public static void main(String[] args) {
Singleton2 singleton = Singleton2.getInstance();
System.out.println(singleton);
}
}

@ -0,0 +1,27 @@
package com.demo.tank.course5.singleton;
/*
* lazy loading
* 线
* */
public class Singleton3 {
private static Singleton3 INSTANCE;
private Singleton3() {
}
public static Singleton3 getInstance() {
if(INSTANCE == null){
INSTANCE = new Singleton3();
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton3 singleton = Singleton3.getInstance();
//多线程 访问可能造成new出多个实例 第一个线程判读为null的时候还没new完对象 第二个线程进来也new了一个对象
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,27 @@
package com.demo.tank.course5.singleton;
/*
* lazy loading
* ,
* */
public class Singleton4 {
private static Singleton4 INSTANCE;
private Singleton4() {
}
public static synchronized Singleton4 getInstance() {
if(INSTANCE == null){
INSTANCE = new Singleton4();
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton4 singleton = Singleton4.getInstance();
//多线程 访问可能造成new出多个实例 第一个线程判读为null的时候还没new完对象 第二个线程进来也new了一个对象
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,29 @@
package com.demo.tank.course5.singleton;
/*
* lazy loading
* ,
* */
public class Singleton5 {
private static Singleton5 INSTANCE;
private Singleton5() {
}
//线程不安全,同3类似
public static Singleton5 getInstance() {
if(INSTANCE == null){
synchronized (Singleton5.class){
INSTANCE = new Singleton5();
}
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton5 singleton = Singleton5.getInstance();
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,29 @@
package com.demo.tank.course5.singleton;
/*
* lazy loading
* ,
* */
public class Singleton6 {
private static volatile Singleton6 INSTANCE;
private Singleton6() {
}
public static synchronized Singleton6 getInstance() {
synchronized (Singleton6.class){
//双重检查
if(INSTANCE == null) {
INSTANCE = new Singleton6();
}
}
return INSTANCE;
}
public static void main(String[] args) {
Singleton6 singleton = Singleton6.getInstance();
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,25 @@
package com.demo.tank.course5.singleton;
/*
* jvm
*
* */
public class Singleton7 {
private Singleton7() {
}
private static class SingletonHolder{
private static Singleton7 INSTANCE = new Singleton7();
}
public static synchronized Singleton7 getInstance() {
return SingletonHolder.INSTANCE;
}
public static void main(String[] args) {
Singleton7 singleton = Singleton7.getInstance();
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(singleton.hashCode())).start();
}
}
}

@ -0,0 +1,15 @@
package com.demo.tank.course5.singleton;
/*
* , 线
* */
public enum Singleton8 {
INSTANCE;
public static void main(String[] args) {
for (int i=0; i< 100; i++){
new Thread(() -> System.out.println(Singleton8.INSTANCE.hashCode())).start();
}
}
}

@ -0,0 +1,26 @@
package com.demo.tank.course5.strategy;
public class Cat implements Comparable<Cat>{
private int weight;
private int height;
public Cat(int weight, int height) {
this.weight = weight;
this.height = height;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
'}';
}
@Override
public int compareTo(Cat c) {
if(this.weight < c.weight) return -1;
else if(this.weight > c.weight) return 1;
else return 0;
}
}

@ -0,0 +1,8 @@
package com.demo.tank.course5.strategy;
public class CatHeightComparator implements Comparator<Cat>{
@Override
public int compareTo(Cat t1, Cat t2) {
return 0;
}
}

@ -0,0 +1,8 @@
package com.demo.tank.course5.strategy;
public class CatWeightComparator implements Comparator<Cat>{
@Override
public int compareTo(Cat t1, Cat t2) {
return 0;
}
}

@ -0,0 +1,5 @@
package com.demo.tank.course5.strategy;
public interface Comparator<T> {
int compareTo(T t1, T t2);
}

@ -0,0 +1,23 @@
package com.demo.tank.course5.strategy;
public class Dog implements Comparable<Dog>{
int food;
public Dog(int food) {
this.food = food;
}
@Override
public String toString() {
return "Dog{" +
"food=" + food +
'}';
}
@Override
public int compareTo(Dog d) {
if(this.food < d.food) return -1;
else if(this.food > d.food) return 1;
else return 0;
}
}

@ -0,0 +1,10 @@
package com.demo.tank.course5.strategy;
public class DogComparator implements Comparator<Dog>{
@Override
public int compareTo(Dog d1, Dog d2) {
if(d1.food < d2.food) return -1;
else if(d1.food > d2.food) return 1;
else return 0;
}
}

@ -0,0 +1,29 @@
package com.demo.tank.course5.strategy;
import java.util.Arrays;
public class Sorter<T> {
public void sort(T[] arr, Comparator<T> comparator){
for (int i = 0; i < arr.length - 1; i++) {
int minPos = i;
for (int j = i+1; j < arr.length; j++) {
minPos = comparator.compareTo(arr[j], arr[minPos]) == -1 ? j : minPos;
}
swap(arr, i, minPos);
}
}
private void swap(T[] arr, int i, int minPos) {
T temp = arr[i];
arr[i] = arr[minPos];
arr[minPos] = temp;
}
public static void main(String[] args) {
Dog[] dogs = new Dog[]{new Dog(5), new Dog(1), new Dog(6)};
Cat[] cats = new Cat[]{new Cat(6,2), new Cat(3,4), new Cat(5,5)};
Sorter<Dog> sorter = new Sorter<>();
sorter.sort(dogs, new DogComparator());
System.out.println(Arrays.toString(dogs));
}
}
Loading…
Cancel
Save