生产者,消费者,单例模式:
public class Test03 {
public static void main(String[] args) {
Apple a = new Apple();
Producer p = new Producer(a);
Consumer c = new Consumer(a);
p.start();
c.start();
}
}
class Apple{
int count = 1;
boolean flag = true;
}
class Producer extends Thread{
Apple a;
public Producer(Apple a) {
this.a = a;
}
public void produce() {
a.count++;
}
public void run() {
while(true) {
synchronized(Apple.class) {
if(a.flag) {
try {
Apple.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
produce();
System.out.println("生产苹果"+a.count);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a.flag = true;
Apple.class.notify();
}
}
}
}
class Consumer extends Thread{
Apple a;
public Consumer(Apple a) {
this.a = a;
}
public int consume() {
return a.count;
}
public void run() {
while(true) {
synchronized(Apple.class) {
if(!a.flag) {
try {
Apple.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("吃掉"+consume());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a.flag = false;
Apple.class.notify();
}
}
}
}
线程状态:
Thread.State | getState() 返回此线程的状态。 |
public class Test04 {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
synchronized("abc") {
for(int n = 1;n<=5;n++) {
System.out.println(getName()+"---"+getState() + "---"+n);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
Thread t2 = new Thread() {
public void run() {
synchronized("abc") {
for(int n = 1;n<=5;n++) {
System.out.println(getName()+"---"+getState() + "---"+n);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
t1.start();
t2.start();
for(int n = 0;n<10;n++) {
System.out.println("t1:" +t1.getState());
System.out.println("t2:" +t2.getState());
}
}
}
线程池:
static ExecutorService | 创建一个使用从无界队列运行的单个工作线程的执行程序。 | ||||
static ExecutorService | newFixedThreadPool(int nThreads) 创建一个线程池,该线程池重用固定数量的从共享无界队列中运行的线程。 | ||||
Future<?> | submit(Runnable task) 提交一个可运行的任务执行,并返回一个表示该任务的未来。 |
shutDown():结束线程池,已经提交的全部保证完成,不准继续提交了。
shutDownNow():结束线程池,已经开始运行的,保证完成,但是还没有运行的,已经提交的,不给运行了,对于没有提交的,不准提交。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//线程池
public class Test05 {
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(2);
//ExecutorService es = Executors.newFixedThreadPool(2);
Runnable r1 = new Runnable() {
@Override
public void run() {
for(int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"...."+i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Runnable r2 = new Runnable() {
public void run() {
for(int n = 0;n < 50;n++) {
System.out.println(Thread.currentThread().getName()+"----" + n);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Runnable r3 = new Runnable() {
public void run() {
for(int n = 0;n < 50;n++) {
System.out.println(Thread.currentThread().getName()+"*****"+n);
/*由于线程名称格式是:pool-1-thread-2这种,n的前面如果不加东西,就会和它连在一起
所以System.out.println(Thread.currentThread().getName()+n);这种写法会很别扭*/
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
es.submit(r1);
es.submit(r2);
es.submit(r3);
es.shutdown();//结束线程池
}
}
单例设计模式:
饿汉式:
//饿汉式
public class Test06 {
public static void main(String[] args) {
Single1 single1 = Single1.getInstance();
}
}
class Single1{
private Single1() {//构造方法私有化,不能外界创建对象
}
private static Single1 s = new Single1();//必须静态
public static Single1 getInstance() {
return s;//必须静态方法,通过类名直接调用.因为外界已经不能创建对象了
}
}
懒汉式:
//懒汉式
public class Test07 {
public static void main(String[] args) {
Single2 s = Single2.getInstance();
}
}
class Single2{
static Single2 s;//必须静态
private Single2() {
//构造方法私有化
}
public static Single2 getInstance() {//必须静态
if(s==null) {
synchronized("abc") {//判断锁比判断s==null更耗费时间,所以加了个外层,提高效率
if(s==null) {
s = new Single2();
}
}
}
return s;
}
}
老汉式:
//老汉式
public class Test08 {
public static void main(String[] args) {
Single3 s = Single3.s;
}
}
class Single3{
private Single3() {
}
public static final Single3 s = new Single3();//public
}
枚举类型:
常用方法:
int | compareTo(E o) 将此枚举与指定的对象进行比较以进行订购。 | ||
int | ordinal() 返回此枚举常数的序数(其枚举声明中的位置,其中初始常数的序数为零)。 | ||
String | name() 返回此枚举常量的名称,与其枚举声明中声明的完全相同。 | ||
String | toString() 返回声明中包含的此枚举常量的名称。 |
实例一:
//枚举
public class Test09 {
public static void main(String[] args) {
MyWeekDay mon = MyWeekDay.MON;
mon.show();
System.out.println(mon.name);
}
}
enum MyWeekDay{
MON("星期一") {
@Override
public void show() {
System.out.println("今天"+getName());
}
},TUE("星期二") {
@Override
public void show() {
System.out.println("今天"+getName());
}
},WEN("星期三") {
@Override
public void show() {
System.out.println("今天"+getName());
}
};
String name;
private MyWeekDay(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void show();
}
实例二:
public class Test10 {
public static void main(String[] args) {
System.out.println(Demo.D1.ordinal());
System.out.println(Demo.D2.ordinal());
System.out.println(Demo.D3.ordinal());
System.out.println(Demo.D1.compareTo(Demo.D2));
System.out.println(Demo.D1.name());
System.out.println(Demo.D1);
Demo[] values = Demo.values();
for(Demo d: values) {
System.out.println(d);
}
}
}
enum Demo{
D1,D2,D3;
public String toString() {
return this.name().toLowerCase();
}
}