请问多线程下对某接口的取值问题

2019-01-10 16:54:11
  1. public class threadtest extends Thread{
  2. private test i;
  3. public threadtest(test i){
  4. this.i = i;
  5. }
  6. @Override
  7. public void run() {
  8. for (int i = 0; i < 5; i++) {
  9. try {
  10. Thread.sleep(1000);
  11. } catch (InterruptedException e) {
  12. // TODO Auto-generated catch block
  13. e.printStackTrace();
  14. }
  15. System.out.println("当前线程:"+Thread.currentThread().getName());
  16. System.out.println(this.i.gettst());
  17. }
  18. }
  19. public static void main(String[] args) {
  20. testA a = new testA();
  21. test t = a;
  22. a.setI(1);
  23. new threadtest(t).start();
  24. a.setI(2);
  25. new threadtest(t).start();
  26. a.setI(3);
  27. new threadtest(t).start();
  28. }
  29. }
  30. 接口类
  31. public interface test {
  32. int gettst()
  33. }
  34. 实现接口
  35. public class testA implements test{
  36. private int i;
  37. @Override
  38. public int gettst() {
  39. // TODO Auto-generated method stub
  40. return i;
  41. }
  42. public void setI(int i) {
  43. this.i = i;
  44. }
  45. }

打印结果

我想每个线程打印的值都是当时赋予的值,而不是全部都统一了..各位大老展示展示


0
1
0

添加评论

正在回复:
取消
5
0
1
0