2016-10-01 20 views
1

我是在一个棘手的情况。我知道这几乎是我现在可以得到的,但我想要做的是实际上创建一个线程(或多个线程)的数组,并容纳队列中线程的数量,例如,我可以容纳一次3个线程,我让第一个3线程运行,然后让其他等待,当有空闲时,例如1是空闲的或终止,另一个可以开始运行。在线队列中等待的主题,Java

另外我想确保线程是否可以运行,如果正在运行的线程与其他线程的性别相同。

Thread myThreads[] = new Thread[LN.GetLine().length()]; 
    int l=LN.GetLine().length(); 
    for (int j = 0; j < LN.GetLine().length(); j++) { 
    String Name = CR.Gender(LN.GetLine().charAt(j)) + j; 
    myThreads[j] = new Thread(new MyThread(Name,LN)); 
    myThreads[j].setPriority(l); 
    System.out.println(myThreads[j].toString()); 
    l--; 
    } 
    for(int b=0;b<LN.GetLine().length();b++){ 
     myThreads[b].start(); 
     synchronized(myThreads[b]){ 
      try{ 
       myThreads[b].wait();  
     }catch(InterruptedException e){ 
      e.printStackTrace(); 
     } 

     } 

     } 

现在,我可以容纳或使1线程运行在一次。

(是的,这是一个机器浴室的问题)

我真正的问题是。如果我编辑功能run()myThread()wait()或只是简单地把System.out.println(getName() + " is Using"); 该线程将如何知道他们的run()函数,其他线程正在运行。

public class MyThread extends Thread { 
public MyThread(String id) { 
super(id); 
} 
public void run(){ 
System.out.println(getName() + " is Using"); 
>>>Put wait if other thread running<<<< 
>>>If can use or not if same gender<<<< 
} 

或者我应该只是实现外部?或把等待外面? 另外我在线程中真的很新,所以我还没有真正探索睡眠和中断。

+1

在一个线程上调用'wait()'是不寻常的,在这种情况下,它没有任何作用。 – Titus

+0

是的,我知道这次我试过了...无论如何我不能杀死/停止一个线程,因为那意味着他们不能使用,也许如果我可以让所有线程等待,并保持等待。直到我恢复他们,如果他们能够使用。 – HerlDerp

+0

考虑使用固定线程池ExecutorService(在您的情况下,池大小将为3)。这种技术不需要创建等待的线程(浪费资源)。 ref:https://docs.oracle.com/javase/7/docs/api/index.html?java/util/concurrent/ExecutorService.html – mangotang

回答

0

您可以在没有wait的情况下执行此操作。

下面是一个例子:

public class MyThread extends Thread { 
    static final Object gender1Lock = new Object(); 
    static final Object gender2Lock = new Object(); 
    int gender; 
    public MyThread(String id, int gender) { 
     super(id); 
     this.gender = gender; 
    } 
    public void run(){ 
     System.out.println(getName() + " is waiting"); 
     if(gender == 1){ 
      synchronized(gender1Lock){ // ocupy gender 1 
       System.out.println(getName() + " is Using"); 
      } 
     }else if(gender == 2){ 
      synchronized(gender1Lock){ // ocupy gender 2 
       System.out.println(getName() + " is Using"); 
      } 
     } 
    } 
} 

由于只有一个在它意味着只有一个给定的性别线程可同时运行的时间的对象线程可以synchronize。这将创建给定性别的所有线程的顺序执行。

这里是一个使用这种线程的例子。

for(int i = 0; i < 20; i++){ 
    new MyThread("Person " + i, (i%2 == 0) ? 1 : 2).start(); 
} 
+0

获得也许我很困惑,但是性别1锁和性别2锁在这种情况下需要是静态的吗?我们不想实现Runnable,而不是扩展Thread?那么使用某种类型的堆栈或... –

+1

执行器服务? –

+0

@HovercraftFullOfEels是的,我刚刚意识到我自己('静态'的东西)。 – Titus

0

我只想用通过Executors.newSingleThreadExecutor();,每一个性别获得了两个单独的线程执行者,然后提交任务到合适的执行人。简单而完成。

如果你只有一间浴室,那么只需要一个执行者。例如:

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 

public class GetInLine { 
    public static void main(String[] args) { 
     List<MyRunnable> myRunnables = new ArrayList<>(); 
     myRunnables.add(new MyRunnable("Bob", false)); 
     myRunnables.add(new MyRunnable("Jill", true)); 
     myRunnables.add(new MyRunnable("Frank", false)); 
     myRunnables.add(new MyRunnable("Amy", true)); 
     myRunnables.add(new MyRunnable("Pete", false)); 
     myRunnables.add(new MyRunnable("Diane", true));   

     ExecutorService myExecutor = Executors.newSingleThreadExecutor(); 
     for (MyRunnable myRunnable : myRunnables) { 
      myExecutor.submit(myRunnable); 
     } 

     myExecutor.shutdown(); 
    } 
} 

public class MyRunnable implements Runnable { 
    private static final int MIN_SLEEP_TIME = 500; 
    private static final int MAX_SLEEP_TIME = 2000; 
    private static final int FEMALE_SLEEP_BONUS = 500; 
    private Random random = new Random(); 
    private String name; 
    private boolean female; 

    public MyRunnable(String name, boolean female) { 
     this.name = name; 
     this.female = female; 
    } 

    public String getName() { 
     return name; 
    } 

    public boolean isFemale() { 
     return female; 
    } 

    @Override 
    public void run() { 
     System.out.println(name + " is using"); 
     try { 
      long sleepTime = MIN_SLEEP_TIME + random.nextInt(MAX_SLEEP_TIME - MIN_SLEEP_TIME); 
      if (female) { 
       sleepTime += FEMALE_SLEEP_BONUS; 
      } 
      Thread.sleep(sleepTime); 
     } catch (InterruptedException e) {} 
     System.out.println(name + " is done"); 
    } 
} 
0

...所以例如,我可以同时容纳3线,我做第一个3线运行,然后让对方等待,当有是免费的,例如1免费或终止另一个可以开始运行。

这可以使用具有固定线程池的Executor来实现。

ExecutorService m = Executors.newFixedThreadPool(3) 
ExecutorService f = Executors.newFixedThreadPool(3) 
for(;;) { 
    Object o = new Object(); 
    m.execute(() -> {...; o.wait(); /* You know notify was called on o at this point hence f run clause is running or has just run */ ...}) 
    f.execute(() -> {...; o.notify(); ...}) 
} 

由于这一个一个batroom的疑问句是单独的一个就会有固定的厕所数量:

ExecutorService m = Executors.newFixedThreadPool(3) 
ExecutorService f = Executors.newFixedThreadPool(3) 
for(;;) { 
    Person male = getNextMale(); 
    m.execute(() -> {...; /* do something with male */ ...}) 

    Person female = getNextFemale(); 
    f.execute(() -> {...; /* do something with female */ ...}) 
} 

要实现你可以使用BlockingQueue实现一个队列。