看了昨天网友给出的多线程示例代码,简单的修改了一下。我想要实现的功能是有一个对象数组,Book[] books。我要对他进行保存。以前是单一的
int num = books.length();
for(int i=0; i<num; i++){
Book book = books[i];
saveBook(book)
}现在多线程了,是不是这样操作?public class ThreadSave implements Runnable { private Book[] books;
public ThreadSave (Book[] books) {
this.books = books;
} private final String robj = "";
private static int i = 0; public void run() {
int num = books.length();
for (; i < num; i++) {
synchronized (robj) {
System.out.println("线程:" + Thread.currentThread() + ",i=" + (i+1));
System.out.flush();
Book book = books[i];
saveBook(book)
}
try {
Thread.sleep(0);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} public static void main(String[] args) throws Exception { ThreadSave ts = new ThreadSave();
for (int i = 0; i < 5; i++) {
new Thread(ts).start();
}
}
这样就是多线程保存了么?比原来的普通for效率要高么?
int num = books.length();
for(int i=0; i<num; i++){
Book book = books[i];
saveBook(book)
}现在多线程了,是不是这样操作?public class ThreadSave implements Runnable { private Book[] books;
public ThreadSave (Book[] books) {
this.books = books;
} private final String robj = "";
private static int i = 0; public void run() {
int num = books.length();
for (; i < num; i++) {
synchronized (robj) {
System.out.println("线程:" + Thread.currentThread() + ",i=" + (i+1));
System.out.flush();
Book book = books[i];
saveBook(book)
}
try {
Thread.sleep(0);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} public static void main(String[] args) throws Exception { ThreadSave ts = new ThreadSave();
for (int i = 0; i < 5; i++) {
new Thread(ts).start();
}
}
这样就是多线程保存了么?比原来的普通for效率要高么?
构造方法是带参数的,ThreadSave ts = new ThreadSave(); 却没有参数
int num = books.length();
for(int i=0; i <num; i++){
Book book = books[i];
saveBook(book)
}
public class ThreadSave implements Runnable { private Book[] books;
public ThreadSave (Book[] books) {
this.books = books;
} public void run() {
int num = books.length();
for (int i; i < num; i++) {
saveBook(books[i])
}
}
} public static void main(String[] args) throws Exception { ThreadSave ts = new ThreadSave(books);
new Thread(ts).start();
}
import java.util.Random;class Book {
}public class ThreadSave implements Runnable {
public void run() {
while (true) {
int bookID;
// 以同步的方式获取下一个需要保存的Book编号
synchronized (lock) {
// 无剩余Book需要保存,退出线程
if (next >= books.length)
return;
bookID = next++;
}
// 非同步的方式保存对象,saveBook本身支持多线程同时执行
System.out.println(Thread.currentThread() + " 保存.." + bookID);
saveBook(books[bookID]);
System.out.println(Thread.currentThread() + " 保存完毕.." + bookID);
}
} private static void saveBook(Book book) {
try {
// 休眠1s-10s,模拟保存工作的耗时和时间的不确定性
Thread.sleep(rd.nextInt(9000) + 1000);
} catch (Exception ex) {
ex.printStackTrace();
}
} private static Book[] books = new Book[10];
static {
for (int i = 0; i < books.length; i++)
books[i] = new Book();
}
private static Object lock = new Object();
private static int next = 0; private static Random rd = new Random(); public static void main(String[] args) {
ThreadSave save = new ThreadSave();
for (int i = 0; i < 5; i++)
new Thread(save).start();
}
}