自定义线程类中的实例变量针对其他线程有共享和不共享两种
一 . 不共享数据
创建测试类:
public class MyThread extends Thread { private int count = 10; public MyThread(String name){ super(); this.setName(name); } @Override public void run() { super.run(); while (count > 0){ count--; System.out.println(this.currentThread().getName() + "count-- : " + count ); } } }
调用:
public class Test { public static void main(String[] args) { try { MyThread first = new MyThread("first"); MyThread second = new MyThread("second"); MyThread third = new MyThread("third"); first.start(); second.start(); third.start(); } catch (Exception e) { e.printStackTrace(); } } }
运行结果:
二 . 共享数据
创建线程类:
public class MyThread extends Thread { private int count = 10; @Override public void run() { super.run(); count--; System.out.println(this.currentThread().getName() + "count-- : " + count); } }
创建测试类:
public class Test { public static void main(String[] args) { try { MyThread myThread = new MyThread(); Thread first = new Thread(myThread,"first"); Thread second = new Thread(myThread,"second"); Thread third = new Thread(myThread,"third"); first.start(); second.start(); third.start(); } catch (Exception e) { e.printStackTrace(); } } }
运行结果:
三 . 非线程安全:
多个线程对同一个对象中的同一个实例变量进行操作时出现值被更改.值不同步的情况,进而影响程序执行流程.
public class UnSafeThread extends Thread{ private static String name; private static String pswd; public static void login(String userName,String userPswd){ try { name = userName; if(userName.equals("aaaa")){ Thread.sleep(5000); } pswd = userPswd; System.out.println("userName = " + name + ", userPassword = " + pswd); }catch (Exception e){ e.printStackTrace(); } }}
public class ALogin extends Thread { @Override public void run(){ UnSafeThread.login("aaaa","aaaaaa"); }}
public class BLogin extends Thread { @Override public void run(){ UnSafeThread.login("bbbb","bbbbbb"); }}
public class Test { public static void main(String[] args) { ALogin aLogin = new ALogin(); aLogin.start(); BLogin bLogin = new BLogin(); bLogin.start(); }}
运行结果:
解决非线程安全问题,还是会使用synchronized,将UnSafeThread类中的代码更改为
synchronized public class UnSafeThread extends Thread{ private static String name; private static String pswd; public static void login(String userName,String userPswd){ try { name = userName; if(userName.equals("aaaa")){ Thread.sleep(5000); } pswd = userPswd; System.out.println("userName = " + name + ", userPassword = " + pswd); }catch (Exception e){ e.printStackTrace(); } }}
四 . i--与println()的异常
创建测试类:
public class MyThread extends Thread { private int count = 10; @Override public void run() { super.run(); System.out.println(this.currentThread().getName() + "count-- : " + count--); }}
运行类:
public class Test { public static void main(String[] args) { MyThread myThread = new MyThread(); Thread t1 = new Thread(myThread); Thread t2 = new Thread(myThread); Thread t3 = new Thread(myThread); Thread t4 = new Thread(myThread); Thread t5 = new Thread(myThread); Thread t6 = new Thread(myThread); Thread t7 = new Thread(myThread); Thread t8 = new Thread(myThread); Thread t9 = new Thread(myThread); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); t7.start(); t8.start(); t9.start(); }}
运行结果:
因为println()方法在内部进行了同步操作,但是i--的操作确实在进入println()之前进行操作,所以有发生非线程安全的问题,所以,为了防止发生非线程安全的问题,还是应使用同步方法.