继承Thread类

有单继承局限

  1. public class Test extends Object {
  2. public static void main(String[] args) throws Exception {
  3. Book book1=new Book("书1",99);
  4. Book book2=new Book("书2",1231);
  5. book1.start();
  6. book2.start();
  7. }
  8. }
  9. //线程操作类
  10. class Book extends Thread{
  11. private String title;
  12. private Integer price;
  13. public Book() {
  14. }
  15. public Book(String title, Integer price) {
  16. this.title = title;
  17. this.price = price;
  18. }
  19. public String getTitle() {
  20. return title;
  21. }
  22. public void setTitle(String title) {
  23. this.title = title;
  24. }
  25. public Integer getPrice() {
  26. return price;
  27. }
  28. public void setPrice(Integer price) {
  29. this.price = price;
  30. }
  31. @Override
  32. public void run() {
  33. System.out.printf("书名 : %s , 价格 : %d \n",this.title,this.price);
  34. }
  35. }

实现Runnable接口

有效避免单继承局限问题,多线程首选

  1. public class Test extends Object {
  2. public static void main(String[] args) throws Exception {
  3. Book book1=new Book("书1",99);
  4. Book book2=new Book("书2",1231);
  5. new Thread(book2).start();
  6. new Thread(book1).start();
  7. }
  8. }
  9. class Book implements Runnable{
  10. private String title;
  11. private Integer price;
  12. public Book() {
  13. }
  14. public Book(String title, Integer price) {
  15. this.title = title;
  16. this.price = price;
  17. }
  18. public String getTitle() {
  19. return title;
  20. }
  21. public void setTitle(String title) {
  22. this.title = title;
  23. }
  24. public Integer getPrice() {
  25. return price;
  26. }
  27. public void setPrice(Integer price) {
  28. this.price = price;
  29. }
  30. @Override
  31. public void run() {
  32. System.out.printf("书名 : %s , 价格 : %d \n",this.title,this.price);
  33. }
  34. }

使用Callable接口实现多线程

JDK1.5开始提供Callable接口 java.util.concurrent.Callable
call方法可以实现线程操作数据的返回,返回数据类型由泛型决定

  1. public interface Callable<V> {
  2. /**
  3. * Computes a result, or throws an exception if unable to do so.
  4. *
  5. * @return computed result
  6. * @throws Exception if unable to compute a result
  7. */
  8. V call() throws Exception;
  9. }

FutureTask继承结构如下:
FutureTask是Runnable的子类,所以仍可以使用Thread启动线程.
1.FutureTask.pdf

注意.Callable只是胜在又返回值.Runnable还是使用最广泛的

  1. public class Test extends Object {
  2. public static void main(String[] args) throws Exception {
  3. FutureTask<String> futureTask=new FutureTask<>(new Book("书1",455));
  4. new Thread(futureTask).start();
  5. for (int i = 0; i < 1000; i++) {
  6. System.out.println("测试语句"+i);
  7. }
  8. System.out.println(futureTask.get());
  9. for (int i = 0; i < 1000; i++) {
  10. System.out.println("阿巴阿巴"+i);
  11. }
  12. }
  13. }
  14. //线程操作类
  15. class Book implements Callable<String>{
  16. private String title;
  17. private Integer price;
  18. public Book() {
  19. }
  20. public Book(String title, Integer price) {
  21. this.title = title;
  22. this.price = price;
  23. }
  24. public String getTitle() {
  25. return title;
  26. }
  27. public void setTitle(String title) {
  28. this.title = title;
  29. }
  30. public Integer getPrice() {
  31. return price;
  32. }
  33. public void setPrice(Integer price) {
  34. this.price = price;
  35. }
  36. @Override
  37. public String call() throws Exception {
  38. for (int i = 0; i < 1000; i++) {
  39. System.out.println("子线程"+i);
  40. }
  41. System.out.printf("书名 : %s , 价格 : %d \n",this.title,this.price);
  42. return "输出完成";
  43. }
  44. }