이것저것 해보다 안되서 문의드립니다.(쓰레드 질문)
나려
현재 제가 구현하려고 하는게
jsp에서 버튼 클릭 하면 큐에 data를 insert 해주고
큐에 값이 들어오면 쓰레드를 실행하여 큐에 들어온 값으로 data 가공하는 기능입니다.
만약 쓰레드가 실행되고 있는 도중에(큐가 비워져 있지 않는 동안) 또다시 버튼을 클릭하면
쓰레드를 실행할 수 없다는 메세지를 띄워주려고 합니다.
쓰레드는 처음 해보는거라 나름대로 이렇게 저렇게 해봤는데 안되는 것 같아서 문의드립니다.
A.jsp에 ① 번 버튼, ②번 버튼이 있습니다.
A.jsp에서 ① 번이나 ②번 버튼을 클릭하면 B.jsp 를 실행합니다.
B.jsp 내용은 다음과 같습니다.
%@ page contentType=text/html; charset=euc-kr %
%
int album_id =111111;
ServletThread s = new ServletThread();
if(s.test(album_id)){
%
쓰레드 실행했습니다.
%
}
else{
%
실행할수 없습니다
%
}
%
ServletThread.javapublic class ServletThread
{
private static int queue[] = new int[1];
private static int front, rear, total ;
static class Threadpool
{
public Threadpool() {
}
public void init(){
front = rear = total = -1;
}
public void setQueue(int album_id){
addElement(album_id);
}
public void addElement(int album_id){
if(isFull()){
System.out.println(큐가 꽉참!);
}
queue[++rear] = album_id;
}
public int getElement(){
if(isEmpty()) {
System.out.println(큐가 비었음!);
}
return queue[++front];
}
public boolean isEmpty() {
if(front==rear) return true;
else return false;
}
public boolean isFull() {
if(rear == queue.length-1) return true;
else return false;
}
public boolean isNextWork() {
if(total==rear) return true;
else return false;
}
public void setEmpty() {
total = rear;
}
}
class PoolTest implements Runnable {
private Threadpool pool;
private String name;
public PoolTest(String name, Threadpool pool){
this.name = name;
this.pool = pool;
}
public void run(){
int tmpInt = 0;
while(!pool.isEmpty()){
int album_id = pool.getElement(); // 큐에서 가져온 data 를 가공하는 로직이 들어갈 부분인데 임시로 loop 돌림
for (int j=0;j25000;j++){
System.out.println(rear=+rear);
System.out.println(total=+total);
}
}
pool.setEmpty();
&nbsbsp; }
}
public boolean test(int album_id){
Threadpool pool = new Threadpool();
if (pool.isNextWork()){
pool.init();
pool.setQueue(album_id);
new Thread(new PoolTest(쓰레드,pool)).start();
return true;
}
else{
return false;
}
}}
}이렇게 해봤는데 A.jsp에서 ① 번 버튼을 클릭하면 쓰레드가 실행 잘되는데
문제는 A.jsp에서 ① 번 버튼을 클릭하여 쓰레드를 실행시킨후 쓰레드가 종료하기전에
②번 버튼을 클릭하면먼저 실행된 쓰레드가 돌고 있기 때문에 실행이 안되어야 하는데
먼저 돌던 쓰레드가 끝나면 또실행이 됩니다.보시고 조언 좀 부탁드립니다.