import java.util.Observable;public class Product extends Observable
{ private String name;
private float price; public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
this.setChanged();
this.notifyObservers(this.name);
} public float getPrice()
{
return price;
} public void setPrice(float price)
{
this.price = price;
this.setChanged();
this.notifyObservers(this.price);
}
}import java.util.Observable;
import java.util.Observer;public class PriceObserver implements Observer
{ private float price = 0.0f;
public void update(Observable o, Object arg)
{
if(arg instanceof Float)
{
price = ((Float)arg).floatValue();
System.out.println("Product price has changed : " + price);
}
}}
import java.util.Observable;
import java.util.Observer;public class NameObserver implements Observer
{
private String name = null;
public void update(Observable o, Object arg)
{
if(arg instanceof String)
{
name = (String)arg;
System.out.println("Product name has changed : " + name);
}
}}
public class ObserverTest
{ public static void main(String[] args)
{
Product p = new Product();
p.setName("apple 4");
p.setPrice(5.52f);
System.out.println(p.getName());
System.out.println(p.getPrice());
p.addObserver(new NameObserver());
p.addObserver(new PriceObserver());
p.setName("apple 5");
p.setPrice(6.15f);
}}
{ private String name;
private float price; public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
this.setChanged();
this.notifyObservers(this.name);
} public float getPrice()
{
return price;
} public void setPrice(float price)
{
this.price = price;
this.setChanged();
this.notifyObservers(this.price);
}
}import java.util.Observable;
import java.util.Observer;public class PriceObserver implements Observer
{ private float price = 0.0f;
public void update(Observable o, Object arg)
{
if(arg instanceof Float)
{
price = ((Float)arg).floatValue();
System.out.println("Product price has changed : " + price);
}
}}
import java.util.Observable;
import java.util.Observer;public class NameObserver implements Observer
{
private String name = null;
public void update(Observable o, Object arg)
{
if(arg instanceof String)
{
name = (String)arg;
System.out.println("Product name has changed : " + name);
}
}}
public class ObserverTest
{ public static void main(String[] args)
{
Product p = new Product();
p.setName("apple 4");
p.setPrice(5.52f);
System.out.println(p.getName());
System.out.println(p.getPrice());
p.addObserver(new NameObserver());
p.addObserver(new PriceObserver());
p.setName("apple 5");
p.setPrice(6.15f);
}}
{
private static Singleton instance = null;
private static String key = "key";
private Singleton()
{
}
public static Singleton getInstance()
{
if(instance == null)
{
synchronized (key)
{
if(instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
public void say(){
System.out.println("Hello world!");
}
}public class SingletonTest
{
public static void main(String[] args)
{
Singleton singleton = Singleton.getInstance();
for (int i = 0; i < 5; i++) {
singleton.say();
}
}
}
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;public class TimeTest
{
private static int i = 0;
public static void main(String[] args)
{
Timer timer = new Timer("MyTimer"); timer.schedule(new TimerTask()
{
public void run()
{
callUsers();
}
}, 0, 1000L);
} public static void callUsers()
{
System.out.println("User" + i);
i ++;
}
public void schedule(TimerTask task, long delay, long period)
{
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(task,delay,period,TimeUnit.MILLISECONDS);
}
}