使用MySQL实现Java*多线程
这是我的第一篇帖子,如果需要的话,请让我知道我是否应该在帖子中添加任何内容 我有一个程序,从我的MySQL数据库中获取URL,并将它们发送到一个类,该类将把图像下载到我的桌面上 我需要获取80000个图像,我想利用多线程技术使其更快 我已经取得了一些进展,但我现在陷入了困境,花了数小时进行研究,因为我对多线程是全新的 问题是,当程序运行时,它会通过一个循环反复处理前5个条目。这恰好是ExecutorFixedPoolSize 谁能帮帮我吗 我的代码如下使用MySQL实现Java*多线程,java,multithreading,Java,Multithreading,这是我的第一篇帖子,如果需要的话,请让我知道我是否应该在帖子中添加任何内容 我有一个程序,从我的MySQL数据库中获取URL,并将它们发送到一个类,该类将把图像下载到我的桌面上 我需要获取80000个图像,我想利用多线程技术使其更快 我已经取得了一些进展,但我现在陷入了困境,花了数小时进行研究,因为我对多线程是全新的 问题是,当程序运行时,它会通过一个循环反复处理前5个条目。这恰好是ExecutorFixedPoolSize 谁能帮帮我吗 我的代码如下 public static voi
public static void main(String[] args) {
try{
countAllAltPicsNotSaved();
System.out.println("Not saved: " + totalAltPicsNotSaved);
Class.forName("com.mysql.jdbc.Driver");
Connection conn =DriverManager.getConnection(dbUrl + dbName, userName, password);
Statement s = conn.createStatement ();
s.executeQuery ("SELECT DISTINCT id from productsaltimages WHERE saved != 1");
ResultSet rs = s.getResultSet();
int saveCounter = 0, altImageCount = 0;
List<Thread> threads = new ArrayList<Thread>();
while (rs.next()) { //Get ID
altImageCount = 0; //Product Alt Image Counter
String id = rs.getString("id"); //Product Table ID
Statement news = conn.createStatement (); //New Conn for get Alt Images From ID
news.executeQuery ("SELECT url from productsaltimages WHERE id ='"+id+"' AND saved != 5"); //Gets query from mySQL
ResultSet newrs = news.getResultSet(); //Resultset for AltImges for ID
ExecutorService executor = Executors.newFixedThreadPool(5);
Runnable task = null;
while (newrs.next()){ //Get Images
altImageCount++; //Increment ID Alt Image Counter
String url = newrs.getString("url");
task = new DownloadImage(url, id, altImageCount);
executor.execute(task);
}
}
} catch (Exception e){
}
}
public static void countAllAltPicsNotSaved() throws Exception {
try{
totalAltPicsNotSaved=0;
Class.forName("com.mysql.jdbc.Driver");
Connection conn =DriverManager.getConnection(dbUrl + dbName, userName, password);
Statement s = conn.createStatement ();
boolean sqlExecuteok = false;
s.executeQuery ("SELECT * from productsaltimages WHERE saved = '0'");
ResultSet rs = s.getResultSet ();
while (rs.next()) {
totalAltPicsNotSaved++;
}
rs.close();
s.close();
conn.close();
} catch (Exception e){
}
}
For my DownloadImage class code is:
public class DownloadImage implements Runnable
{
String url, id, threadName;
private int altImageCount = 0;
private static String userName = "owner";
private static String password = "hello123";
private static String dbUrl = "jdbc:mysql://localhost/";
private static String dbName = "shop";
private static String dbClass = "com.mysql.jdbc.Driver";
public DownloadImage(String url, String id, int altImageCount)
{
this.url = url;
this.id = id;
this.altImageCount = altImageCount;
}
public void run()
{
while(true)
{
File file=new File("D:\\FBShop_AltImages\\" + id + "\\");
boolean exists = file.exists();
if (!exists) {
// Create multiple directories
boolean success = (new File("D:\\FBShop_AltImages\\" + id + "\\")).mkdirs();
}
String newFilename = "D:\\FBShop_AltImages\\" + id + "\\" + id + "_" + altImageCount + ".jpg";
try {
try {
BufferedImage image = null;
URL imgurl = new URL(url);
URLConnection con = imgurl.openConnection();
con.setConnectTimeout(50 * 10000);
con.setReadTimeout(50 * 10000);
InputStream in = con.getInputStream();
image = ImageIO.read(in);
ImageIO.write(image, "jpg", new File(newFilename));
} catch (Exception e) {
System.out.println("Error getting image: " + url);
}
try {
//UpdateTable
Class.forName("com.mysql.jdbc.Driver");
String updatesql = "UPDATE productsaltimages SET saved = '1' WHERE id = '"+id+"'";
Connection conn =DriverManager.getConnection(dbUrl + dbName, userName, password);
Statement ups = conn.createStatement ();
int val = ups.executeUpdate(updatesql);
System.out.println("Task Complete: " + url);
try {
Thread.sleep(5000);
} catch (Exception e) {
}
} catch (Exception e) {
}
} catch (Exception e) {
}
//System.out.println("Thread Finished: " + threadName);
}
}
}
publicstaticvoidmain(字符串[]args){
试一试{
countAllAltPicsNotSaved();
System.out.println(“未保存:+totalAltPicsNotSaved”);
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn=DriverManager.getConnection(dbUrl+dbName、用户名、密码);
语句s=conn.createStatement();
s、 executeQuery(“从保存的productsaltimages中选择不同的id!=1”);
ResultSet rs=s.getResultSet();
int saveCounter=0,altImageCount=0;
List threads=new ArrayList();
while(rs.next()){//Get ID
altImageCount=0;//产品Alt图像计数器
String id=rs.getString(“id”);//产品表id
语句news=conn.createStatement();//用于从ID获取Alt图像的新conn
news.executeQuery(“从productsaltimages中选择url,其中id=”+id+“,并保存!=5”);//从mySQL获取查询
ResultSet newrs=news.getResultSet();//用于ID的AltImges的ResultSet
ExecutorService executor=Executors.newFixedThreadPool(5);
可运行任务=null;
while(newrs.next()){//获取图像
altImageCount++;//增量ID Alt Image计数器
字符串url=newrs.getString(“url”);
任务=新下载图像(url、id、altImageCount);
执行者。执行(任务);
}
}
}捕获(例外e){
}
}
public static void countAllAltPicsNotSaved()引发异常{
试一试{
totalAltPicsNotSaved=0;
Class.forName(“com.mysql.jdbc.Driver”);
Connection conn=DriverManager.getConnection(dbUrl+dbName、用户名、密码);
语句s=conn.createStatement();
布尔值sqlExecuteok=false;
s、 executeQuery(“从保存的productsaltimages中选择*='0'”);
ResultSet rs=s.getResultSet();
while(rs.next()){
totalAltPicsNotSaved++;
}
rs.close();
s、 close();
康涅狄格州关闭();
}捕获(例外e){
}
}
对于我的下载,图像类代码为:
公共类DownloadImage实现可运行
{
字符串url、id、线程名称;
私有int altImageCount=0;
私有静态字符串userName=“owner”;
私有静态字符串password=“hello123”;
私有静态字符串dbUrl=“jdbc:mysql://localhost/";
私有静态字符串dbName=“shop”;
私有静态字符串dbClass=“com.mysql.jdbc.Driver”;
公共下载图像(字符串url、字符串id、int altImageCount)
{
this.url=url;
this.id=id;
this.altImageCount=altImageCount;
}
公开募捐
{
while(true)
{
文件文件=新文件(“D:\\FBShop\U AltImages\\”+id+“\\”;
boolean exists=file.exists();
如果(!存在){
//创建多个目录
布尔值成功=(新文件(“D:\\FBShop\u AltImages\\”+id+“\\”).mkdirs();
}
String newFilename=“D:\\FBShop\u AltImages\\”+id+“\\\”+id+“\\”+altImageCount+”.jpg”;
试一试{
试一试{
BuffereImage图像=空;
URL imgurl=新URL(URL);
URLConnection con=imgurl.openConnection();
con.设置连接超时(50*10000);
con.setReadTimeout(50*10000);
InputStream in=con.getInputStream();
image=ImageIO.read(in);
write(图像,“jpg”,新文件(newFilename));
}捕获(例外e){
System.out.println(“获取图像时出错:+url”);
}
试一试{
//可更新
Class.forName(“com.mysql.jdbc.Driver”);
String updatesql=“UPDATE productsaltimages SET saved='1'其中id='”+id+“'”;
Connection conn=DriverManager.getConnection(dbUrl+dbName、用户名、密码);
语句ups=conn.createStatement();
int val=ups.executeUpdate(updatesql);
System.out.println(“任务完成:+url”);
试一试{
睡眠(5000);
}捕获(例外e){
}
}捕获(例外e){
}
}捕获(例外e){
}
//System.out.println(“线程完成:+threadName”);
}
}
}
什么时候结束?删除while true,你应该是好的 对不起,我不知道发生了什么事以及为什么会这样..因为我知道我从网上复制粘贴了一些代码。。我想我在那里学到了教训不要那样做!!我把它去掉了,一切都很顺利。。花了几个小时,但至少我研究了多线程,学到了很多东西,哈哈。。谢谢你,这是为我修好的。@JeffreyHolmes如果你想学习线程,你应该读一本书-实践中的并发-这是最好的。干杯@尤金·洛尔,微妙的暗示:p
while(true){
//lots of code inside you Runnable
Thread.sleep(5000);
//After the Thread will sleep it will restart it's work again, and again..
}