先从线程的建立提及.线程的建立一共有两种形式:
--------------------------------------------------------------------------------
一种是继承自Thread类.Thread 类是一个具体的类,即不是抽象类,该类封装了线程的行为。要建立一个线程,程序员必须建立一个从 Thread 类导出的新类。程序员经过覆盖 Thread 的 run() 函数来完成有用的工做。用户并不直接调用此函数;而是经过调用 Thread 的 start() 函数,该函数再调用 run()。
例如:
public class Test extends Thread{
public Test(){
}
public static void main(String args[]){
Test t1 = new Test();
Test t2 = new Test();
t1.start();
t2.start();
}
public void run(){
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"**|----|**"+i);
} }
}
结果:t1和t2两个线程就交叉执行的,即t1执行一下子,t2执行一下子。
--------------------------------------------------------------------------------
另外一种是实现Runnable接口,此接口只有一个函数,run(),此函数必须由实现了此接口的类实现。
例如:
public class Test implements Runnable{
Thread thread1;
Thread thread2;
public Test(){
thread1 = new Thread(this,"1");
thread2 = new Thread(this,"2");
}
public static void main(String args[]){
Test t = new Test();
t.startThreads();
}
public void run(){
//do thread's things
}
public void startThreads(){
thread1.start();
thread2.start();
}
}
两种建立方式看起来差异不大,可是弄不清楚的话,也许会将你的程序弄得一团糟。二者区别有如下几点:
1.当你想继承某一其它类时,你只能用后一种方式.
2.第一种由于继承自Thread,只建立了自身对象,可是在数量上,须要几个线程,就得建立几个自身对象;第二种只建立一个自身对象,却建立几个Thread对象.而两种方法重大的区别就在于此,请你考虑:若是你在第一种里建立数个自身对象而且start()后,你会发现好像synchronized不起做用了,已经加锁的代码块或者方法竟然同时能够有几个线程进去,并且一样一个变量,竟然能够有好几个线程同时能够去更改它。(例以下面的代码)这是由于,在这个程序中,虽然你起了数个线程,但是你也建立了数个对象,并且,每一个线程对应了每一个对象也就是说,每一个线程更改和占有的对象都不同,因此就出现了同时有几个线程进入一个方法的现象,其实,那也不是一个方法,而是不一样对象的相同的方法。因此,这时候你要加锁的话,只能将方法或者变量声明为静态,将static加上后,你就会发现,线程又能管住方法了,同时不可能有两个线程进入一样一个方法,那是由于,如今不是每一个对象都拥有一个方法了,而是全部的对象共同拥有一个方法,这个方法就是静态方法。
而你若是用第二种方法使用线程的话,就不会有上述的状况,由于此时,你只建立了一个自身对象,因此,自身对象的属性和方法对于线程来讲是共有的。
所以,建议最好用后一种方法来使用线程。
public class mainThread extends Thread{
int i=0;
public static void main(String args[]){
mainThread m1 = new mainThread();
mainThread m2 = new mainThread();
mainThread m3 = new mainThread();
mainThread m4 = new mainThread();
mainThread m5 = new mainThread();
mainThread m6 = new mainThread();
m1.start();
m2.start();
m3.start();
m4.start();
m5.start();
m6.start();
}
public synchronized void t1(){
i=++i;
try{
Thread.sleep(500);
}
catch(Exception e){}
//每一个线程都进入各自的t1()方法,分别打印各自的i
System.out.println(Thread.currentThread().getName()+" "+i);
}
public void run(){
synchronized(this){
while (true) {
t1();
}
}
}
}java
--------------------------------------------------------------------------------
下面咱们来说synchronized的4种用法吧:
1.方法声明时使用,放在范围操做符(public等)以后,返回类型声明(void等)以前.即一次只能有一个线程进入该方法,其余线程要想在此时调用该方法,只能排队等候,当前线程(就是在synchronized方法内部的线程)执行完该方法后,别的线程才能进入.
例如:
public synchronized void synMethod() {
//方法体
}
2.对某一代码块使用,synchronized后跟括号,括号里是变量,这样,一次只有一个线程进入该代码块.例如:
public int synMethod(int a1){
synchronized(a1) {
//一次只能有一个线程进入
}
}
3.synchronized后面括号里是一对象,此时,线程得到的是对象锁.例如:
public class MyThread implements Runnable {
public static void main(String args[]) {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt, "t1");
Thread t2 = new Thread(mt, "t2");
Thread t3 = new Thread(mt, "t3");
Thread t4 = new Thread(mt, "t4");
Thread t5 = new Thread(mt, "t5");
Thread t6 = new Thread(mt, "t6");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
}
public void run() {
synchronized (this) {
System.out.println(Thread.currentThread().getName());
}
}
}程序员
对于3,若是线程进入,则获得对象锁,那么别的线程在该类全部对象上的任何操做都不能进行.在对象级使用锁一般是一种比较粗糙的方法。为何要将整个对象都上锁,而不容许其余线程短暂地使用对象中其余同步方法来访问共享资源?若是一个对象拥有多个资源,就不须要只为了让一个线程使用其中一部分资源,就将全部线程都锁在外面。因为每一个对象都有锁,能够以下所示使用虚拟对象来上锁:
class FineGrainLock {
MyMemberClass x, y;
Object xlock = new Object(), ylock = new Object();
public void foo() {
synchronized(xlock) {
//access x here
}
//do something here - but don't use shared resources
synchronized(ylock) {
//access y here
}
}
public void bar() {
synchronized(this) {
//access both x and y here
}
//do something here - but don't use shared resources
}
}
4.synchronized后面括号里是类.例如:
class ArrayWithLockOrder{
private static long num_locks = 0;
private long lock_order;
private int[] arr;
public ArrayWithLockOrder(int[] a)
{
arr = a;
synchronized(ArrayWithLockOrder.class) {//-----------------------------------------这里
num_locks++; // 锁数加 1。
lock_order = num_locks; // 为此对象实例设置惟一的 lock_order。
}
}
public long lockOrder()
{
return lock_order;
}
public int[] array()
{
return arr;
}
}
class SomeClass implements Runnable
{
public int sumArrays(ArrayWithLockOrder a1,
ArrayWithLockOrder a2)
{
int value = 0;
ArrayWithLockOrder first = a1; // 保留数组引用的一个
ArrayWithLockOrder last = a2; // 本地副本。
int size = a1.array().length;
if (size == a2.array().length)
{
if (a1.lockOrder() > a2.lockOrder()) // 肯定并设置对象的锁定
{ // 顺序。
first = a2;
last = a1;
}
synchronized(first) { // 按正确的顺序锁定对象。
synchronized(last) {
int[] arr1 = a1.array();
int[] arr2 = a2.array();
for (int i=0; i<size; i++)
value += arr1[i] + arr2[i];
}
}
}
return value;
}
public void run() {
//...
}
}
对于4,若是线程进入,则线程在该类中全部操做不能进行,包括静态变量和静态方法,实际上,对于含有静态方法和静态变量的代码块的同步,咱们一般用4来加锁.
以上4种之间的关系:
锁是和对象相关联的,每一个对象有一把锁,为了执行synchronized语句,线程必须可以得到synchronized语句中表达式指定的对象的锁,一个对象只有一把锁,被一个线程得到以后它就再也不拥有这把锁,线程在执行完synchronized语句后,将得到锁交还给对象。
在方法前面加上synchronized修饰符便可以将一个方法声明为同步化方法。同步化方法在执行以前得到一个锁。若是这是一个类方法,那么得到的锁是和声明方法的类相关的Class类对象的锁。若是这是一个实例方法,那么此锁是this对象的锁。
数组
--------------------------------------------------------------------------------jvm
下面谈一谈一些经常使用的方法:
wait(),wait(long),notify(),notifyAll()等方法是当前类的实例方法,
wait()是使持有对象锁的线程释放锁;
wait(long)是使持有对象锁的线程释放锁时间为long(毫秒)后,再次得到锁,wait()和wait(0)等价;
notify()是唤醒一个正在等待该对象锁的线程,若是等待的线程不止一个,那么被唤醒的线程由jvm肯定;
notifyAll是唤醒全部正在等待该对象锁的线程.
在这里我也重申一下,咱们应该优先使用notifyAll()方法,由于唤醒全部线程比唤醒一个线程更容易让jvm找到最适合被唤醒的线程.
对于上述方法,只有在当前线程中才能使用,不然报运行时错误java.lang.IllegalMonitorStateException: current thread not owner.
--------------------------------------------------------------------------------ide
下面,我谈一下synchronized和wait()、notify()等的关系:
1.有synchronized的地方不必定有wait,notify
2.有wait,notify的地方必有synchronized.这是由于wait和notify不是属于线程类,而是每个对象都具备的方法,并且,这两个方法都和对象锁有关,有锁的地方,必有synchronized。
另外,请注意一点:若是要把notify和wait方法放在一块儿用的话,必须先调用notify后调用wait,由于若是调用完wait,该线程就已经不是current thread了。以下例:函数
import java.lang.Runnable;
import java.lang.Thread;
public class DemoThread
implements Runnable {
public DemoThread() {
TestThread testthread1 = new TestThread(this, "1");
TestThread testthread2 = new TestThread(this, "2");
testthread2.start();
testthread1.start();
}
public static void main(String[] args) {
DemoThread demoThread1 = new DemoThread();
}
public void run() {
TestThread t = (TestThread) Thread.currentThread();
try {
if (!t.getName().equalsIgnoreCase("1")) {
synchronized (this) {
wait();
}
}
while (true) {
System.out.println("@time in thread" + t.getName() + "=" +
t.increaseTime());
if (t.getTime() % 10 == 0) {
synchronized (this) {
System.out.println("****************************************");
notify();
if (t.getTime() == 100)
break;
wait();
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class TestThread
extends Thread {
private int time = 0;
public TestThread(Runnable r, String name) {
super(r, name);
}
public int getTime() {
return time;
}
public int increaseTime() {
return++time;
}
}
下面咱们用生产者/消费者这个例子来讲明他们之间的关系:
public class test {
public static void main(String args[]) {
Semaphore s = new Semaphore(1);
Thread t1 = new Thread(s, "producer1");
Thread t2 = new Thread(s, "producer2");
Thread t3 = new Thread(s, "producer3");
Thread t4 = new Thread(s, "consumer1");
Thread t5 = new Thread(s, "consumer2");
Thread t6 = new Thread(s, "consumer3");
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
t6.start();
}
}
class Semaphore
implements Runnable {
private int count;
public Semaphore(int n) {
this.count = n;
}
public synchronized void acquire() {
while (count == 0) {
try {
wait();
}
catch (InterruptedException e) {
//keep trying
}
}
count--;
}
public synchronized void release() {
while (count == 10) {
try {
wait();
}
catch (InterruptedException e) {
//keep trying
}
}
count++;
notifyAll(); //alert a thread that's blocking on this semaphore
}
public void run() {
while (true) {
if (Thread.currentThread().getName().substring(0,8).equalsIgnoreCase("consumer")) {
acquire();
}
else if (Thread.currentThread().getName().substring(0,8).equalsIgnoreCase("producer")) {
release();
}
System.out.println(Thread.currentThread().getName() + " " + count);
}
}
}
生产者生产,消费者消费,通常没有冲突,但当库存为0时,消费者要消费是不行的,但当库存为上限(这里是10)时,生产者也不能生产.请好好研读上面的程序,你必定会比之前进步不少.
上面的代码说明了synchronized和wait,notify没有绝对的关系,在synchronized声明的方法、代码块中,你彻底能够不用wait,notify等方法,可是,若是当线程对某一资源存在某种争用的状况下,你必须适时得将线程放入等待或者唤醒.ui