一、递归遍历路径下的文件
public class GetFile {
public static void main(String[] args) {
String path="D:/ss";
getPath(path);
}
private static void getPath(String path){
File file = new File(path);
File[] files = file.listFiles();
if(files==null){
return;
}
for (File file1 : files) {
if(file1.isFile()){
System.out.println(file1.getPath());
}else {
getPath(file1.getPath());
}
}
}
二、冒泡排序
public class Order {
public static void main(String[] args) {
int [] arry={12,32,45,21,2,6,22};
int temp;
for (int i = 0; i < arry.length-1; i++) {
for (int j = 0; j < arry.length-i-1; j++) {
if(arry[j]>arry[j+1]){
temp=arry[j+1];
arry[j+1]=arry[j];
arry[j]=temp;
}
}
}
for (int i : arry) {
System.out.println(i);
}
}
}
三、单例模式
饿汉式
public class SingleDemo01 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance.equals(instance1));
}
}
class Singleton{
private Singleton(){
}
private final static Singleton instance =new Singleton();
public static Singleton getInstance(){
return instance;
}
}
懒汉式(线程不安全)
public class SingleDemo03 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
class Singleton{
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance(){
if (instance==null){
instance=new Singleton();
}
return instance;
}
}
懒汉式(线程安全、同步方法、效率低)
public class SingleDemo04 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
class Singleton{
private static Singleton instance;
private Singleton(){
}
public static synchronized Singleton getInstance(){
if (instance==null){
instance=new Singleton();
}
return instance;
}
}
懒汉式(双重检查,线程安全,效率高)
public class SingleDemo05 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance1 = Singleton.getInstance();
System.out.println(instance==instance1);
}
}
class Singleton{
private static volatile Singleton instance;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null){
synchronized (Singleton.class){
if (instance==null){
instance=new Singleton();
}
}
}
return instance;
}
}
生产者、消费者
public class A {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
try {
for (int i = 0; i < 10; i++) {
data.increment();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
class Data{
private int num=0;
public synchronized void increment() throws InterruptedException {
while(num!=0){
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName()+"---"+num);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while(num==0){
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName()+"---"+num);
this.notifyAll();
}
}