简体   繁体   English

为什么我的应用程序暂停线程,我该如何解决这个问题?

[英]Why is my app suspending threads and how can I fix this?

Here is my code: 这是我的代码:

public class CustomSwipeAdaptor extends PagerAdapter {

    private int[] image_resources={R.drawable.aa,R.drawable.bb,R.drawable.cc};
    private Context ctx;
    private LayoutInflater layoutInflater;

    public CustomSwipeAdaptor(Context ctx)
    {
        this.ctx=ctx;
    }

    @Override
    public int getCount() {
        return image_resources.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view==(LinearLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
        View item_view=layoutInflater.inflate(R.layout.swipe_layout,container,false);
        ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
        imageView.setImageResource(image_resources[position]);
        container.addView(item_view);
        return item_view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout)object);
    }
}


public class Chapter1 extends AppCompatActivity{

    ViewPager viewPager;
    CustomSwipeAdaptor adaptor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chapter1);
        viewPager=(ViewPager)findViewById(R.id.view_pager);
        adaptor=new CustomSwipeAdaptor(this);
        viewPager.setAdapter(adaptor);
    }


}

Here is my logcat: 这是我的logcat:

06-12 18:45:05.631 341-349/com.example.kips.itapps W/art: Suspending all threads took: 7.170ms 
06-12 18:45:27.677 341-349/com.example.kips.itapps W/art: Suspending all threads took: 6.168ms 
06-12 18:47:31.577 341-349/com.example.kips.itapps W/art: Suspending all threads took: 7.052ms 
06-12 18:48:36.275 341-349/com.example.kips.itapps W/art: Suspending all threads took: 5.650ms 
06-12 18:56:21.208 341-349/com.example.kips.itapps W/art: Suspending all threads took: 8.523ms

This is largely harmless. 这在很大程度上是无害的。 From here 这里开始

The heap for Java code is a shared resource. Java代码堆是一个共享资源。 For the GC pause in ART, threads must be suspended so they aren't changing the heap and breaking GC invariants. 对于ART中的GC暂停,必须暂停线程,以便它们不会更改堆并破坏GC不变量。 To suspend threads a thread local variable is modified by the GC and then the threads observe this change and relinquish the mutator lock, the GC will then hold the lock exclusively and run. 要挂起线程,GC会修改线程局部变量,然后线程会观察到此更改并放弃mutator锁定,然后GC将独占地保持锁定并运行。 As threads are suspended whilst other threads relinquish their share of the mutator lock, its important for jank that threads respond in a timely manner. 当线程被挂起而其他线程放弃它们的mutator锁的份额时,它对jank的重要性是线程及时响应。 5ms is nearly 1/3rd of a 16ms frame and so ART warns when thread suspension is slow. 5ms几乎是16ms帧的1/3,因此当线程悬挂缓慢时,ART会发出警告。 As +Aladin Q​says this may happen with a lot of load. 正如+ Aladin Q所说,这可能会带来很多负担。 If its happening frequently, or without load, it may be evidence of a bug that may cause jank. 如果它经常发生或没有负载,它可能是一个可能导致jank的bug的证据。

Unless you have serious problems preventing your app from accomplishing its goals, it is benign, along with several other similar log outputs . 除非你有严重的问题阻止你的应用程序实现其目标,否则它是良性的,以及其他几个类似的日志输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM