简体   繁体   中英

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:

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. For the GC pause in ART, threads must be suspended so they aren't changing the heap and breaking GC invariants. 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. 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. 5ms is nearly 1/3rd of a 16ms frame and so ART warns when thread suspension is slow. As +Aladin Q​says this may happen with a lot of load. If its happening frequently, or without load, it may be evidence of a bug that may cause jank.

Unless you have serious problems preventing your app from accomplishing its goals, it is benign, along with several other similar log outputs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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