Java 为什么定时器在for循环内部不工作?

Java 为什么定时器在for循环内部不工作?,java,Java,我在列表的每个元素中都设置了这个计时器。 对于for循环,我希望在每次迭代中执行每个元素的每个计时器事件。然而,代码执行了,但计时器似乎不起作用,所有元素都会立即列出。这是什么原因 我已经对代码进行了总结,只包含了必要的部分 public class Main { public static void main(String[] args) { new Main(); } public Main() { playList playLis

我在列表的每个元素中都设置了这个计时器。 对于for循环,我希望在每次迭代中执行每个元素的每个计时器事件。然而,代码执行了,但计时器似乎不起作用,所有元素都会立即列出。这是什么原因

我已经对代码进行了总结,只包含了必要的部分

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        playList playList = new playList();

        Song song1 = new Song("song 1", "artist 1", 2000);
        playList.add(song1);
        Song song2 = new Song("song 2", "artist 2", 3000);
        playList.add(song2);
        Song song3 = new Song("song 3", "artist 3", 3500);
        playList.add(song3);
        Song song4 = new Song("song 4", "artist 4", 3500);
        playList.add(song4);

        playList.playThisList();

    }
}

class Song {
    String title;
    String artist;
    int duration;

    public Song(String title, String artist, int duration) {
        this.title = title;
        this.artist = artist;
        this.duration = duration;
    }
    public String toString() {
        return this.artist + ", " + this.title;
    }

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run () { stop(); } };


    public void play() {
        timer.scheduleAtFixedRate(task, duration, duration);
        System.out.println("Now Playing");
    }

    public void stop() {
        timer.cancel();
    }
}

class playList () {
    void playThisList()  {
        System.out.println("Playlist");
        Node node = head;
        for (int i = 0; i < count; i++) {
            node.song.play();
            node = node.next;
        }
    }
}
公共类主{
公共静态void main(字符串[]args){
新的Main();
}
公用干管(){
playList playList=新建播放列表();
Song song1=新歌(“歌曲1”,“艺术家1”,2000年);
播放列表。添加(歌曲1);
Song song2=新歌(“歌曲2”,“艺术家2”,3000);
播放列表。添加(歌曲2);
Song song3=新歌(“歌曲3”,“艺术家3”,3500);
播放列表。添加(歌曲3);
Song song4=新歌(“歌曲4”,“艺术家4”,3500);
播放列表。添加(歌曲4);
playList.playThisList();
}
}
班歌{
字符串标题;
弦乐艺术家;
int持续时间;
公共歌曲(弦乐标题、弦乐艺术家、整数持续时间){
this.title=标题;
这个艺术家=艺术家;
这个。持续时间=持续时间;
}
公共字符串toString(){
返回this.artist+“,”+this.title;
}
定时器=新定时器();
TimerTask任务=新的TimerTask(){
public void run(){stop();}};
公共游戏{
timer.scheduleAtFixedRate(任务、持续时间、持续时间);
System.out.println(“正在播放”);
}
公共停车场(){
timer.cancel();
}
}
类播放列表(){
void playThisList(){
System.out.println(“播放列表”);
节点=头部;
for(int i=0;i

我知道我可以使用
Thread.sleep()
,但我仍然想了解为什么这里不可能使用计时器,或者在这种情况下我如何使用计时器?

我做了一些更改,我认为这些更改可以满足您的需要,同时又能保持代码的“精神”。也许把这个和你的比较一下,看看是否有帮助

public class Main {

    public static void main(String[] args) {
        new Main().run();
    }

    public void run() {
        playList playList = new playList();

        Song song1 = new Song("song 1", "artist 1", 2000);
        playList.add(song1);
        Song song2 = new Song("song 2", "artist 2", 3000);
        playList.add(song2);
        Song song3 = new Song("song 3", "artist 3", 3500);
        playList.add(song3);
        Song song4 = new Song("song 4", "artist 4", 3500);
        playList.add(song4);

        playList.playThisList();

    }
}

class Song {

    String title;
    String artist;
    int duration;

    public Song(String title, String artist, int duration) {
        this.title = title;
        this.artist = artist;
        this.duration = duration;
    }

    @Override
    public String toString() {
        return this.artist + ", " + this.title;
    }

    public void play() {
        System.out.println("Now Playing: " + this);
    }

}

class playList {

    Timer timer = new Timer();
    Node head = null;

    void add(Song s) {
        head = new Node(head, s);
    }

    void playThisList() {
        System.out.println("Playlist");
        playThisNode(head);
    }

    void playThisNode(Node node) {
        node.song.play();
        final Node next = node.next;
        if (next != null) {
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    playThisNode(next);
                }
            }, node.song.duration);
        } else {
            System.out.println("Done");
            timer.cancel();
        }
    }

    class Node {

        public Node(Node next, Song song) {
            this.song = song;
            this.next = next;
        }
        final Song song;
        final Node next;
    }
}

它确实以相反的顺序播放歌曲,但我相信你可以解决这个问题

你想让每首歌重复播放吗?要同时播放所有歌曲吗?我从你在问题中发布的代码中得到了这样的印象,但我只是想确保我没有弄错。这段代码不会按原样编译,但如果我猜到缺失的部分,它会立即对所有四首歌曲调用
播放
,启动四个独立计时器,执行除了停止计时器之外什么都不做的任务。这些定时器同时运行,当最后一个定时器触发时(3500ms后),程序退出。正如Abra所说,试着更详细地解释一下你想要发生什么。我有一个名为
song
的元素链表。每首歌曲都有一个
play
方法,该方法有一个计时器,如代码所示。我希望能够遍历链表,为每个元素执行
play
方法(及其计时器)。当我执行代码时,它立即执行所有行,并且计时器似乎被忽略。我不明白为什么。谢谢。@PeterHull已经向您解释了为什么在他的评论中似乎忽略了计时器。计时器不会被忽略,它们会立即停止,因为这就是您对它们进行编码的方式。你的代码可以工作。因此,您似乎希望代码执行其他操作,但不知道如何编写代码来实现所需的功能。如果这是真的,那么请回答您的问题并编写您希望代码执行的操作。是否希望所有歌曲同时播放?还是你想让他们一个接一个地玩?您希望每首歌只播放一次还是多次播放?