泛型3 |
|
|
package generic;
import java.util.Vector;
public class GenericMethod {
public static void main(String[] args) {
GenericMethod g = new GenericMethod();
String s = g.getT("huyu", "huyu");
System.out.println(s);
Vector<Integer> v = new Vector<Integer>();
v.add(12);
v.add(44);
v.add(90);
g.getT1(v);
System.out.println("2111111");
Vector<? extends Number> v1 =v;
g.getT1(v1);
}
public <T> T getT(T a, T b) {
T rt;
if (a.equals(b))
rt = a;
else rt = null;
return rt;
}
public void getT1(Vector<? extends Number> v){
for(Number n:v){
System.out.println(n);
}
}
}
|
泛型2 |
|
|
package generic;
import java.util.Date;
public class GenericClass {
public static void main(String[] args) {
Person<String> p = new Person<String>();
p.setId(0);
p.setP("huyu");
System.out.println(p.getId()+" "+p.getP());
@SuppressWarnings("rawtypes")
Person p1 = new Person();
p1.setId(12);
p1.setP(new Date());
System.out.println(p1.getId()+" "+p1.getP());
}
}
class Person<T>{
private int id;
private T p;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public T getP() {
return p;
}
public void setP(T p) {
this.p = p;
}
}
|
泛型1 |
|
|
package generic;
public class Test {
public static void main(String[] args) {
Point<Integer> p = new Point<Integer>();
p.setX(12);
p.setY(232);
p.show();
}
}
class Point<T extends Number>{
private T x;
private T y;
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
public void show(){
System.out.println("x="+x+"y="+y);
}
}
|
死锁2 |
|
|
package sync;
public class TestDeadLock {
public static void main(String[] args) {
StringBuffer s=new StringBuffer("huyu");
MyClass m = new MyClass(s);
m.start();
synchronized (s) {
try {
m.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyClass extends Thread{
private StringBuffer s;
public MyClass(StringBuffer s) {
this.s = s;
}
public void run(){
synchronized (s) {
s.reverse();
}
System.out.println("sub thread is over!");
}
}
|
加锁方法的测试 |
|
|
package sync;
public class Test implements Runnable{
private int i=0;
public synchronized void m1(){
try {
Thread.sleep(1000);
i=1000;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("同步方法里面的:i="+i);
}
public void m2(){
System.out.println("未加锁的方法i="+i);
}
@Override
public void run() {
m1();
}
public static void main(String[] args) {
Test tt =new Test();
Thread s =new Thread(tt);
s.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tt.m2();
}
}
//结果:未加锁的方法i=0
同步方法里面的:i=1000
package sync;
public class Test implements Runnable{
private int i=0;
public synchronized void m1(){
try {
i=1000;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("同步方法里面的:i="+i);
}
public void m2(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
i=1;
}
@Override
public void run() {
m1();
}
public static void main(String[] args) {
Test tt =new Test();
Thread s =new Thread(tt);
s.start();
tt.m2();
}
}
//结果:同步方法里面的:i=1
package sync;
public class Test implements Runnable{
private int i=0;
public synchronized void m1(){
try {
i=1000;
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("同步方法里面的:i="+i);
}
public synchronized void m2(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
i=1;
}
@Override
public void run() {
m1();
}
public static void main(String[] args) {
Test tt =new Test();
Thread s =new Thread(tt);
s.start();
tt.m2();
}
}
//结果:同步方法里面的:i=1000
总结:1.互斥锁,锁定某个对象后,保证在某段时间内只有一个线程能够减弱某段代码里面,不能保证不会进入其他的代码里面。
如果另外的代码段也枷锁了,则同时只能执行一个加锁的代码段。
2.写的方法一般要加锁,读的方法一般不加锁,加锁了反而影响效率
|
deadLock' |
|
|
package sync;
public class DeadLock implements Runnable {
private int flag = 1;
private Object o1= new Object(),o2 = new Object();
public static void main(String[] args) {
DeadLock d1 = new DeadLock();
DeadLock d2 = new DeadLock();
d1.flag=1;
d2.flag=0;
Thread t1 = new Thread(d1);
Thread t2 = new Thread(d2);
t1.start();
t2.start();
}
@Override
public void run() {
System.out.println("flag = "+flag);
if(flag == 1){
synchronized (o1) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (o2) {
System.out.println("1");
}
}
}
if(flag == 0){
synchronized (o2) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (o1) {
System.out.println("0");
}
}
}
}
}
|
synchronized |
|
|
package sync;
public class TestSync implements Runnable {
Timer t = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
@Override
public void run() {
t.add(Thread.currentThread().getName());
}
}
class Timer {
private static int num = 0;
public synchronized void add(String name) {//在执行这个方法的过程中锁定当前对象,另外的线程不能访问这段
//代码,但是可以访问其他的代码
synchronized (this) {//在执行这段代码的过程中锁定当前对象
num++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name + "你是第" + num + "个!");
}
}
}
|