私有构造方法可以通过静态方法进行构造,用类名.方法();就可以直接调用构造方法了。
package org.demo09;
class Recycler{
private Recycler(){
System.out.println("文件被成功删除!!!");
}
public static Recycler makeRecycler(){
return new Recycler();
}
}
public class Play {
public static void main(String args[]){
Recycler.makeRecycler();
}
}
·同步and死锁
1.java中可以通过同步代码的方式进行加锁操作,同步实现有两种方式:
·同步代码块 :用synchronized关键字进行同步代码块的声明,但是在使用此操
作时必须指出到底要锁定的是那个对象,一般一当前对象为主:
synchronized(对象){//一般都是将this进行锁定
同步代码;
}
·同步方法:
package org.synchronize;
class MyThread implements Runnable {
private int ticket = 10;
public void run() {
for (int i = 0; i < 1; i++) {
this.stat();
}
}
public synchronized void stat() {
if (ticket > 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程开始:" + this.ticket-- );
}
}
}
实际上java定义的完整格式:
[访问权限(public|protected|default|private])]
[static]([final]|[abstract])[synchronized][native]
返回值类型
方法名称(参数列表) throws[异常1,异常2,异常3...]
·死锁:在程序中过多的同步就会产生死锁,那么死锁属于程序并行时发生的一种特殊状态
死锁实例:
package org.lock;
class Ab {
public synchronized void say(Ba b) {
System.out.println("Ba要和Ab说话");
b.talk();
}
public synchronized void talk() {
System.out.println("Ba->Ab");
}
}
class Ba {
public synchronized void say(Ab a) {
System.out.println("Ab要和Ba说话");
a.talk();
}
public synchronized void talk() {
System.out.println("Ab->Ba");
}
}
public class LockTest implements Runnable{
Ab a=new Ab();
Ba b=new Ba();
public LockTest(){
new Thread(this).start();
a.say(b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new LockTest();
}
public void run() {
b.say(a);
}
}
生成者-消费者问题
容器或者存放产品的空间类:
package org.producer;
public class Info {
private String person="小明喜欢吃";
private String name="苹果";
boolean flag=false;
public String getName() {
return name;
}
public synchronized void set(String name,String person){
if(flag==false){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.setName(name);
this.setperson(person);
flag=false;
this.notify();
}
public synchronized void get(){
if(flag==true){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(""+this.getName()+"->"+this.getperson());
flag=true;
this.notify();
}
public void setName(String name) {
this.name = name;
}
public String getperson() {
return person;
}
public void setperson(String person) {
this.person = person;
}
}
生成者:
package org.producer;
public class Producer implements Runnable{
private Info info=null;
public Producer(Info info) {
this.info=info;
}
public void run(){
for(int i=0;i<20;i++){
if(i%2==1){
info.set( "苹果","小明喜欢吃");
}else{
info.set( "橘子","小红喜欢吃");
}
}
}
}
消费者:
package org.producer;
public class Consumer implements Runnable {
private Info info=null;
public Consumer(Info info) {
this.info = info;
}
public void run() {
for(int i=0;i<20;i++)
info.get();
}
}
测试类:
package org.producer;
public class Test {
public static void main(String[] args) {
Info info=new Info();
Producer pro=new Producer(info);
Consumer con=new Consumer(info);
new Thread(pro).start();
new Thread(con).start();
}
}