-----------android培训、java培训、java学习型技术博客、期待与您交流!------------
在此,分享一下自己学习JAVA的学习心得。有不对的地方请帮忙改正,也希望对想学java的同学有帮助!
JAVA语言基础
多线程(二)
同步函数用的锁:
1)函数需要被对象调用。那么函数都有一个所属对象引用。就是this。所以同步函数使用的锁是this。
2)静态的同步方法,使用的锁是该方法所在类的字节码文件对象。 类名.class。
Java代码:
锁为this时:
class Ticket implements Runnable
{
private int tick = 100;
Object obj = new Object();
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(this)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
}
}
}
}
else
while(true)
show();
}
public synchronized void show()//this
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
}
}
}
class ThisLockDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}
静态的同步方法:
class Ticket implements Runnable
{
private static int tick = 100;
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(Ticket.class)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
}
}
}
}
else
while(true)
show();
}
public static synchronized void show()
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....show.... : "+ tick--);
}
}
}
class StaticMethodDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}
同步中出现的死锁现象:
两把锁嵌套使用则有可能出现死锁。
java代码:
class Test implements Runnable
{
private boolean flag;//标志
Test(boolean flag)
{
this.flag = flag;
}
public void run()
{
if(flag)
{
while(true)
{
synchronized(MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+"...if locka ");
synchronized(MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"..if lockb");
}
}
}
}
else
{
while(true)
{
synchronized(MyLock.lockb)
{
System.out.println(Thread.currentThread().getName()+"..else lockb");
synchronized(MyLock.locka)
{
System.out.println(Thread.currentThread().getName()+".....else locka");
}
}
}
}
}
}
class MyLock
{
static Object locka = new Object();
static Object lockb = new Object();
}
class DeadLockTest
{
public static void main(String[] args)
{
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
t2.start();
}
}
同步中嵌套同步:
<span style="font-size:18px;">class Ticket implements Runnable
{
private int tick = 1000;
Object obj = new Object();
boolean flag = true;
public void run()
{
if(flag)
{
while(true)
{
synchronized(obj)
{
show();
}
}
}
else
while(true)
show();
}
public synchronized void show()//this
{
synchronized(obj)
{
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"....code : "+ tick--);
}
}
}
}
class DeadLockDemo
{
public static void main(String[] args)
{
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}</span>
单例设计模式:
1、为了避免其他程序过多建立该类对象。先禁止其他程序建立该类对象
2、还为了让其他程序可以访问到该类对象,只好在本类中,自定义一个对象。
3、为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式。
代码体现过程:
1、将构造函数私有化。
2、在类中创建一个本类对象。
3、提供一个方法可以获取到该对象。
饿汉式:
<span style="font-size:18px;">class Single
{
private static final Single s = new Single();
private Single(){}
public static Single getInstance()
{
return s;
}
}</span>
java代码:
懒汉式:
class Single
{
private static Single s = null;
private Single(){}
public static Single getInstance()
{
if(s==null)
{
synchronized(Single.class)
{
if(s==null)
//--->A;
s = new Single();
}
}
return s;
}
}
class SingleDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
线程通信:
定义:就是多个线程在操作同一个资源,但是操作的动作不同。
例程:
知识点:
1)wait(),notify(),notifyAll(),用来操作线程为什么定义在了Object类中?
1,这些方法存在与同步中。
2,使用这些方法时必须要标识所属的同步的锁。
3,锁可以是任意对象,所以任意对象调用的方法一定定义Object类中。
2)wait(),sleep()有什么区别?
1,wait():释放cpu执行权,释放锁。
2, sleep():释放cpu执行权,不释放锁。
java代码:
生产者消费者的例子:
class ProducerConsumerDemo
{
public static void main(String[] args)
{
Resource r = new Resource();
Producer pro = new Producer(r);
Consumer con = new Consumer(r);
Thread t1 = new Thread(pro);
Thread t2 = new Thread(pro);
Thread t3 = new Thread(con);
Thread t4 = new Thread(con);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class Resource
{
private String name;
private int count = 1;
private boolean flag = false;
// t1 t2
public synchronized void set(String name)
{
while(flag)
try{this.wait();}catch(Exception e){}
this.name = name+"--"+count++;
System.out.println(Thread.currentThread().getName()+"...生产者.."+this.name);
flag = true;
this.notifyAll();
}
// t3 t4
public synchronized void out()
{
while(!flag)
try{wait();}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"...消费者........."+this.name);
flag = false;
this.notifyAll();
}
}
class Producer implements Runnable
{
private Resource res;
Producer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.set("+商品+");
}
}
}
class Consumer implements Runnable
{
private Resource res;
Consumer(Resource res)
{
this.res = res;
}
public void run()
{
while(true)
{
res.out();
}
}
}
停止线程:
1.定义循环结束标记
因为线程运行代码一般都是循环,只要控制了循环即 可。
2. 使用interrupt(中断)方法。
该方法是结束线程的冻结状态,使线程回到 运行状态中来。
注:stop方法已经过时不再使用
知识点:
如何停止线程?
答:只有run方法结束,线程才会停止。开启多线程运行,运行代码通常是循环结构。
只要控制住循环,就可以让run方法结束,也就是线程结束。
特殊情况:
当线程处于了冻结状态。就不会读取到标记。那么线程就不会结束。当没有指定的方式让冻结的线程恢复到运行状态是,这时需要对冻结进行清除。强制让线程恢复到运行状态中来。这样就可以操作标记让线程结束。
Thread类提供该方法 interrupt();
java代码:
class StopThread implements Runnable
{
private boolean flag =true;
public void run()
{
while(flag)
{
System.out.println(Thread.currentThread().getName()+"....run");
}
}
public void changeFlag()
{
flag = false;
}
}
class StopThreadDemo
{
public static void main(String[] args)
{
StopThread st = new StopThread();
Thread t1 = new Thread(st);
Thread t2 = new Thread(st);
t1.setDaemon(true);
t2.setDaemon(true);
t1.start();
t2.start();
int num = 0;
while(true)
{
if(num++ == 60)
{
break;
}
System.out.println(Thread.currentThread().getName()+"......."+num);
}
System.out.println("over");
}
}
线程类的其他方法:
1)setPriority(int num)
2)setDaemon(boolean b)
3)join()
4)toString()
java代码:
class Join implements Runnable
{
public void run()
{
for (int x = 0; x < 70 ; x++ )
{
System.out.println(Thread.currentThread().toString()+"++++"+x);//toString();返回该线程的字符串表示形式,包括线程名称、优先级和线程组。
Thread.yield();
}
}
}
class JoinDemo
{
public static void main(String[] args) throws Exception
{
Join j = new Join();
Thread t1 = new Thread(j);
Thread t2 = new Thread(j);
t1.start();
//当A线程执行到了B线程的.join()方法是,A就会等待。等B线程都执行完,A才会执行。
//join可以用来零时加入线程执行。
//使用时需要对join抛出异常
t1.join();
t2.start();
/*
setPriority();设置优先级
MAX_PRIORITY 最高优先级
MIN_PRIORITY 最低优先级
NORM_PRIORITY 默认优先级
*/
t2.setPriority(Thread.MAX_PRIORITY);//优先级
for (int x = 0; x < 70 ; x++ )
{
System.out.println(Thread.currentThread().getName()+"++++"+x);
}
System.out.println("over");
}
}